https://codeforces.com/contest/1208
f[0]=a, f[1]=b, f[n]=f[n-1]^f[n-2], 求f[n]
可以发现,3个一周期
1 #include<iostream> 2 #include<sstream> 3 #include<fstream> 4 #include<algorithm> 5 #include<cstring> 6 #include<iomanip> 7 #include<cstdlib> 8 #include<cctype> 9 #include<vector> 10 #include<string> 11 #include<cmath> 12 #include<ctime> 13 #include<stack> 14 #include<queue> 15 #include<map> 16 #include<set> 17 #define mem(a,b) memset(a,b,sizeof(a)) 18 #define random(a,b) (rand()%(b-a+1)+a) 19 #define ll long long 20 #define ull unsigned long long 21 #define e 2.71828182 22 #define Pi acos(-1.0) 23 #define ls(rt) (rt<<1) 24 #define rs(rt) (rt<<1|1) 25 #define lowbit(x) (x&(-x)) 26 using namespace std; 27 int read() 28 { 29 int s=1,x=0; 30 char ch=getchar(); 31 while(!isdigit(ch)) {if(ch==‘-‘) s=-1;ch=getchar();} 32 while(isdigit(ch)) {x=10*x+ch-‘0‘;ch=getchar();} 33 return x*s; 34 } 35 36 int main() 37 { 38 int test=read(),c; 39 while(test--) 40 { 41 int a=read(),b=read(),n=read(); 42 c=a^b; 43 if(n%3==0) printf("%d\n",a); 44 else if(n%3==1) printf("%d\n",b); 45 else printf("%d\n",c); 46 } 47 }
给定n个数,求最短区间[l,r]使得去掉[l,r]区间内的数使得剩下的数两两互不相同。
由于数据范围不大(n<=2000),时间又给了两秒,自然想到暴力
1 #include<iostream> 2 #include<sstream> 3 #include<fstream> 4 #include<algorithm> 5 #include<cstring> 6 #include<iomanip> 7 #include<cstdlib> 8 #include<cctype> 9 #include<vector> 10 #include<string> 11 #include<cmath> 12 #include<ctime> 13 #include<stack> 14 #include<queue> 15 #include<map> 16 #include<set> 17 #define mem(a,b) memset(a,b,sizeof(a)) 18 #define random(a,b) (rand()%(b-a+1)+a) 19 #define ll long long 20 #define ull unsigned long long 21 #define e 2.71828182 22 #define Pi acos(-1.0) 23 #define ls(rt) (rt<<1) 24 #define rs(rt) (rt<<1|1) 25 #define lowbit(x) (x&(-x)) 26 using namespace std; 27 const int MAXN=2e3+5; 28 int n,a[MAXN]; 29 map<int,int> tmp,pool; 30 int read() 31 { 32 int s=1,x=0; 33 char ch=getchar(); 34 while(!isdigit(ch)) {if(ch==‘-‘) s=-1;ch=getchar();} 35 while(isdigit(ch)) {x=10*x+ch-‘0‘;ch=getchar();} 36 return x*s; 37 } 38 /*void showtmp() 39 { 40 cout<<"tmp:\n"; 41 for(auto &it:tmp) 42 cout<<it.first<<‘:‘<<it.second<<endl; 43 } 44 void showpool() 45 { 46 cout<<"pool:\n"; 47 for(auto &it:pool) 48 cout<<it.first<<‘:‘<<it.second<<endl; 49 }*/ 50 int main() 51 { 52 int n=read(); 53 for(int i=1;i<=n;++i) 54 { 55 a[i]=read(); 56 if(tmp.count(a[i])) tmp[a[i]]++; 57 else tmp[a[i]]=1; 58 } 59 60 int uni=tmp.size(); 61 if(uni==n) return 0*printf("%d",0); 62 bool flag=false; 63 int ans=-1,lt,lp;//lt保存枚举[l,r]区间的长度,lp是剩下的长度 64 //tmp保存枚举的数及相应的个数,pool保存剩下的数及相应的个数 65 for(int i=n-uni;!flag;++i)//r 66 { 67 tmp.clear(),pool.clear(),lp=n-i,lt=i; 68 for(int j=1;j<=i;++j) 69 { 70 if(tmp.count(a[j])) tmp[a[j]]++; 71 else tmp[a[j]]=1; 72 } 73 for(int j=i+1;j<=n;++j) 74 { 75 if(pool.count(a[j])) pool[a[j]]++; 76 else pool[a[j]]=1; 77 } 78 if(pool.size()==lp) 79 { 80 flag=true; 81 ans=i; 82 } 83 for(int j=i+1;j<=n;++j) 84 { 85 if(pool.count(a[j-lt])) pool[a[j-lt]]++; 86 else pool[a[j-lt]]=1; 87 if(tmp.count(a[j])) tmp[a[j]]++; 88 else tmp[a[j]]=1; 89 if(pool[a[j]]==1) pool.erase(a[j]); 90 else pool[a[j]]--; 91 if(tmp[a[j-lt]]==1) tmp.erase(a[j-lt]); 92 else tmp[a[j-lt]]--; 93 /*cout<<"i: "<<i<<" j: "<<j<<" lt: "<<lt<<endl; 94 showtmp();showpool();*/ 95 if(pool.size()==lp) 96 { 97 flag=true; 98 ans=i;break; 99 } 100 101 } 102 103 } 104 cout<<ans<<endl; 105 }
还是学习下高效一点的写法吧:
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)
原文:https://www.cnblogs.com/wangzhebufangqi/p/11415562.html