月赛30 I-区间异或
区间异或
https://ac.nowcoder.com/acm/contest/9667/I
区间异或和
前言:
我认为这是一道贪心的题目,因为数据量很小,完全不需要使用高级数据结构,只需要一个数组len[i]记录长度为i的区间的最大异或和即可,然后查询时直接for循环查询即可,时间复杂度最大为O(nm),对付这道题绰绰有余!
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define re register
const int maxn = 3e3+7;
int n,m,a[maxn],x,len[maxn],temp,l;
int main()
{
scanf("%d%d",&n,&m);
for(int i = 0;i < n;i++)
scanf("%d",&a[i]);
for(int i = 0;i < n;i++)
{
temp = a[i];
l = 1;
len[l] = max(a[i],len[l]);
for(int j = i+1;j < n;j++)
{
l++;
temp ^= a[j];
len[l] = max(len[l],temp);
}
}
while(m--)
{
scanf("%d",&x);
re int i;
for(i = 1;i <= n;i++)
if(len[i]>x)break;
if(i>n)puts("-1");
else printf("%d\n",i);
}
}
查看9道真题和解析
