Codeforces Round #676 (Div. 2)
A
按位枚举。
#include<iostream> #include<cstring> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } int T,A,B; int main(){ T=read(); while(T--){ A=read(),B=read();int Ans=0; for(int i=30;i>=0;i--){ bool a=A&(1<<i),b=B&(1<<i); int cnt=a+b; if(cnt==2||cnt==0) continue; Ans+=(1<<i); }printf("%d\n",Ans); }return 0; }
B
分类讨论。
#include<iostream> #include<cstring> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second #define int long long using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } using namespace std; const int MAXN=205; int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; int n,tmp[MAXN][MAXN],b[MAXN][MAXN];char str[MAXN];bool vis[MAXN][MAXN];queue<pii> que; bool bfs(){ while(!que.empty())que.pop(); memset(vis,0,sizeof(vis));vis[1][1]=vis[1][2]=vis[2][1]=1; que.push(mp(1,2));que.push(mp(2,1)); while(!que.empty()){ pii now=que.front();que.pop();int x=now.fi,y=now.se; for(int i=0;i<4;i++){ int nx=x+dx[i],ny=y+dy[i]; if(nx>=1&&nx<=n&&ny>=1&&ny<=n) if(!vis[nx][ny]&&(b[nx][ny]==2||b[nx][ny]==b[x][y])) vis[nx][ny]=true,que.push(mp(nx,ny)); } } return vis[n][n]; } bool SRY2(pii x,pii y){ int res=0; for(int i=0;i<=10;i++) res+=i;for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) b[i][j]=tmp[i][j]; b[x.fi][x.se]^=1;b[y.fi][y.se]^=1;return !bfs(); } struct Union{ int f[11];void init(){for(int i=1;i<=10;i++) f[i]=i;return;} int find(int x){return f[x]==x?x:f[x]=find(f[x]);} void merge(int x,int y){int t1=find(x),t2=find(y);f[t2]=t1;return;} }S; bool SRY1(pii x){ int res=0; for(int i=0;i<=10;i++) res+=i; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) b[i][j]=tmp[i][j]; b[x.fi][x.se]^=1;return !bfs(); } vector<pii> vec;bool ff; void Read(){ } signed main(){ int T=read(); while(T--){ Read(); n=read();ff=0; for(int i=1;i<=n;i++){ scanf("%s",str+1); for(int j=1;j<=n;j++) if(str[j]=='0') tmp[i][j]=0; else if(str[j]=='1') tmp[i][j]=1; else tmp[i][j]=2; } for(int i=1;i<=n;i++)for(int j=1;j<=n;j++) b[i][j]=tmp[i][j]; if(!bfs()){printf("0\n");continue;}vec.clear();vec.pb(mp(1,2)),vec.pb(mp(2,1)),vec.pb(mp(n-1,n)),vec.pb(mp(n,n-1)); for(auto XX:vec) if(SRY1(XX)) {printf("1\n%lld %lld\n",XX.first,XX.second);ff=true;break;}if(ff) continue; for(auto XX:vec) for(auto YY:vec) if(!ff&&SRY2(XX,YY)){printf("2\n%lld %lld\n%lld %lld\n",XX.first,XX.second,YY.first,YY.second);ff=true;break;} if(ff) continue; continue; }return 0; }
C
简单构造。
#include<iostream> #include<cstring> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } const int MAXN=1e5+11; char str[MAXN];int N; int main(){ scanf("%s",str+1);N=strlen(str+1); N++; printf("3\n"); printf("L 2\n");printf("R 2\n");N=(N-2)*2+2; printf("R %d\n",N-1);return 0; }
D
大力讨论。
#include<iostream> #include<cstring> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> #include<climits> #define int long long #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } int T,X,Y,C[7],Minn; signed main(){ //freopen("D.in","r",stdin); T=read(); while(T--){ X=read(),Y=read(); for(int i=1;i<=6;i++) C[i]=read();Minn=LLONG_MAX; for(int i=0;i<=1;i++){ for(int j=0;j<=1;j++){ for(int k=0;k<=1;k++){ int curx1=1,curx2=0,curx3=-1,cury1=1,cury2=1,cury3=0,cos1=C[1],cos2=C[2],cos3=C[3]; if(i==1) curx1=-1,cury1=-1,cos1=C[4]; if(j==1) curx2=0,cury2=-1,cos2=C[5]; if(k==1) curx3=1,cury3=0,cos3=C[6]; int x,y,z; //X=0 x=0,y=(Y-cury1*x)/cury2,z=(X-curx1*x)/curx3; if(x>=0&&y>=0&&z>=0) Minn=min(Minn,x*cos1+y*cos2+z*cos3); //cerr<<"y:"<<y<<" z:"<<z<<" "<<cos1<<" "<<cos2<<" "<<cos3<<endl; //X=A1 x=X,y=(Y-cury1*x)/cury2,z=(X-curx1*x)/curx3; if(x>=0&&y>=0&&z>=0) Minn=min(Minn,x*cos1+y*cos2+z*cos3); //X=A2 x=Y,y=(Y-cury1*x)/cury2,z=(X-curx1*x)/curx3; if(x>=0&&y>=0&&z>=0) Minn=min(Minn,x*cos1+y*cos2+z*cos3); //X=-A1 x=-X,y=(Y-cury1*x)/cury2,z=(X-curx1*x)/curx3; if(x>=0&&y>=0&&z>=0) Minn=min(Minn,x*cos1+y*cos2+z*cos3); //X=-A2 x=-Y,y=(Y-cury1*x)/cury2,z=(X-curx1*x)/curx3; if(x>=0&&y>=0&&z>=0) Minn=min(Minn,x*cos1+y*cos2+z*cos3); } } } printf("%lld\n",Minn); //return 0; }return 0; }
E
考虑我们将每个数的权值定为 $1,-1$。设总长度为 $n$ ,$-1$ 的个数 $m$。可以发现 $n+m\mod 3=1$ !,且只要不是 $+-+-+-$ 等就可以符合条件,证明可以归纳即可。
直接 $dp$ 即可。
#include<iostream> #include<cstring> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> #include<climits> #include<bitset> #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second #define int long long using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } const int MAXN=2e5+11; int N,A[MAXN],f[MAXN][3][2][2];//sum 1/0 ok? signed main(){ //freopen("in2.txt","r",stdin); N=read();for(int i=1;i<=N;i++) A[i]=read();int cur=N%3; memset(f,-127/3,sizeof(f));f[1][cur][1][1]=A[1];f[1][(1+cur)%3][0][1]=-A[1]; if(N==1){printf("%lld\n",A[1]);return 0;} for(int i=1;i<N;i++){ for(int j=0;j<=2;j++){ for(int k=0;k<=1;k++){ for(int z=0;z<=1;z++){ for(int s=0;s<=1;s++){ int now=s*2-1,las=k*2-1; int ok=(s^k)&z,sum=(j+(now==-1))%3; f[i+1][sum][s][ok]=max(f[i+1][sum][s][ok],f[i][j][k][z]+A[i+1]*now); } } } } } int Maxn=max(f[N][1][1][0],f[N][1][0][0]); printf("%lld\n",Maxn);return 0; }