C题
用map超时,mmp map一直超时写了两个小时,单纯的数组就行
思路:有n条指令,值为r
存下执行第i条指令的时候的值r,若重复则输出“No”
否则“Yes”
//C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;
typedef long long llong;
const int MAXN = 10000 + 10;
typedef struct
{
int p, v, k;
} node;
bool book[MAXN][260];
int main()
{
int t, n, i, r, sgn;
char str[7];
int p, v, k;
node a[MAXN];
scanf("%d", &t);
getchar();
while(t--)
{
scanf("%d", &n);
getchar();
for(i = 1; i <= n; i++)
{
scanf("%s", str);
getchar();
if(str[1] == 'd')
{
scanf("%d", &k);
getchar();
p = 1;
a[i] = (node){p, v, k};
continue;
}
scanf("%d%d", &v, &k);
getchar();
if(str[1] == 'e')
{
p = 2;
}
else if(str[1] == 'n')
{
p = 3;
}
else if(str[1] == 'l')
{
p = 4;
}
else if(str[1] == 'g')
{
p = 5;
}
a[i] = (node){p, v, k};
}
memset(book, false, sizeof(book));
i = 1; r = 0;
book[i][r] = true;
sgn = 1;
while(i <= n)
{
p = a[i].p;
v = a[i].v;
k = a[i].k;
if(p == 1)
{
r += k;
r %= 256;
i++;
}
else if(p == 2)
{
i = (r == v) ? k : i + 1;
}
else if(p == 3)
{
i = (r != v) ? k : i + 1;
}
else if(p == 4)
{
i = (r < v) ? k : i + 1;
}
else if(p == 5)
{
i = (r > v) ? k : i + 1;
}
if(book[i][r] == true)
{
sgn = 0;
break;
}
book[i][r] = true;
}
if(sgn == 1)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
K
异或运算,要求找到数量最多的集合,集合中任意两个数异或值皆小于这两个数。
//K
#include <bits/stdc++.h>
using namespace std;
int a[100];
bool cmp(int c,int d)
{
return c>d;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int tp,nu;
memset(a,0,sizeof(a));
scanf("%d",&nu);
while(nu--)
{
scanf("%d",&tp);
int cnt=0;
while(tp!=0)
{
tp>>=1;
cnt++;
}
a[cnt]++;
}
sort(a,a+100,cmp);
printf("%d\n",a[0]);
}
return 0;
}