标签:blank sig sort str 预处理 pre target val .com
题目链接:hdu_5968_异或密码
题意:
中午,不解释
题解:
前缀处理一下异或值,然后上个二分查找就行了,注意是unsigned long long
1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;i++) 3 using namespace std; 4 typedef long long ll; 5 6 const int N=111; 7 int t,dp[N],a[N],n,m,tp; 8 9 10 struct dt 11 { 12 int val,len; 13 bool operator<(const dt &b)const 14 { 15 return val<b.val; 16 } 17 }s[N*N],ttp; 18 bool cmp(dt a,dt b) 19 { 20 if(a.val==b.val)return a.len>b.len; 21 return a.val<b.val; 22 } 23 int main() 24 { 25 scanf("%d",&t); 26 while(t--) 27 { 28 scanf("%d",&n); 29 F(i,1,n)scanf("%d",a+i); 30 F(i,1,n)dp[i]=dp[i-1]^a[i]; 31 int ed=0; 32 F(j,1,n)F(k,j,n) 33 { 34 s[++ed].val=dp[k]^dp[j-1]; 35 s[ed].len=k-j+1; 36 } 37 sort(s+1,s+1+ed,cmp); 38 scanf("%d",&m); 39 F(i,1,m) 40 { 41 scanf("%d",&tp); 42 ttp.val=tp; 43 int now1=lower_bound(s+1,s+1+ed,ttp)-s,ans; 44 int now2=now1-1; 45 now2=lower_bound(s+1,s+1+ed,s[now2])-s; 46 if(now1>ed)now1=ed; 47 int n1=abs(s[now1].val-tp),n2=abs(s[now2].val-tp); 48 if(n1>n2)ans=now2; 49 else if(n1<n2)ans=now1; 50 else if(s[now1].len>s[now2].len)ans=now1; 51 else ans=now2; 52 printf("%d\n",s[ans].len); 53 } 54 puts(""); 55 } 56 return 0; 57 }
标签:blank sig sort str 预处理 pre target val .com
原文地址:http://www.cnblogs.com/bin-gege/p/6033743.html