Codeforces 1720D1 Xor-Subsequence (easy version)

Problem Link

The most apparant solution to this problem would be dynamic programming with O ( n 2 ) \mathcal O (n^2) O(n2) complexity, where each f i f_i fi would take O ( n ) \mathcal O(n) O(n) time to calculate.

The key to this problem is the domain of a i a_i ai, which is only 200 200 200. Since a ⊕ x ∈ [ a − x , a + x ] a \oplus x \in [a-x,a+x] ax[ax,a+x], only the indice in range i − 400 i-400 i400 would possibly contribute to the computation of f f f. Therefore, we can narrow down the complexity to O ( 400 n ) \mathcal O(400n) O(400n)

#include
#include
#include
#include
using namespace std;
const int Maxn=3e5+10;
const int Maxm=512;
int a[Maxn],f[Maxn];
int n;
inline int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
int main()
{
	// freopen("in.txt","r",stdin);
	int T=read();
	while(T--)
	{
		n=read();
		for(int i=0;i<n;++i)
		a[i]=read();
		int ans=1;
		for(int i=0;i<n;++i)
		{
			f[i]=1;
			for(int j=max(0,i-512);j<i;++j)
			if(f[j]+1>f[i] && (a[j]^i)<(a[i]^j))f[i]=f[j]+1;
			ans=max(ans,f[i]);
		}
		printf("%d\n",ans);
		fill(f,f+1+n,0);
	}
	return 0;
}

你可能感兴趣的:(题解,#Codeforces,动态规划)