Topic link:
http://codeforces.com/contest/1154
A. Restoring Three Numbers
Attention should be paid to the meaning of the title: a+b is not - a+b;
The code is as follows:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF=0x3f3f3f3f; const int maxn=1e2+5; int n; ll x[4]; int main() { for (int i=0;i<4;i++) { scanf("%lld",&x[i]); } int loc=-1; for (int i=0;i<4;i++) { if(2*x[i]==(x[(i+1)%4]+x[(i+2)%4]+x[(i+3)%4])) { loc=i; break; } } printf("%lld %lld %lld\n",x[loc]-x[(loc+1)%4],x[loc]-x[(loc+2)%4],x[loc]-x[(loc+3)%4]); return 0; }
B. Make Them Equal
Calculate the types of different numbers, and then discuss them by classification.
If type > 3, direct output - 1
If type== 3, see if the absolute difference between left and right numbers is equal to the absolute difference between the middle numbers.
If kind==2, then there must be a solution, because the minimum, then try to narrow the scope, if the difference is even, then the answer needs / 2, if it is odd, then output directly.
If type== 0, output 0
The code is as follows:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF=0x3f3f3f3f; const int maxn=1e2+5; int n; int a[maxn]; int num[maxn]; int main() { int kind=0; memset (num,0,sizeof(num)); scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d",&a[i]); if(num[a[i]]==0) { kind++; } num[a[i]]++; } if(kind==1) { printf("0\n"); return 0; } if(kind==2) { int cnt=0; int b[5]; for (int i=0;i<=100;i++) { if(num[i]) { b[cnt++]=i; } } if((b[0]+b[1])%2) { printf("%d\n",abs(b[0]-b[1])); } else printf("%d\n",abs(b[0]-b[1])/2); return 0; } if(kind==3) { int cnt=0; int b[5]; for (int i=0;i<=100;i++) { if(num[i]) { b[cnt++]=i; } } if(b[2]-b[1]==b[1]-b[0]) { printf("%d\n",b[1]-b[0]); } else printf("-1\n"); } if(kind>=4) printf("-1\n"); return 0; }
C. Gourmet Cat
The simulation starts at one day of the week, then starts to simulate, and then finds the maximum value.
The code is as follows:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF=0x3f3f3f3f; const int maxn=1e2+5; int n; ll numa,numb,numc; ll ans=-1; int main() { scanf("%lld%lld%lld",&numa,&numb,&numc); for (int i=1;i<=7;i++) { ll tnuma=numa,tnumb=numb,tnumc=numc; ll temp=0; int ok=0; for (int j=i;j<=7;j++) { if((j==1||j==4||j==7)) { if(tnuma) { tnuma--; temp++; } else { ok=1; break; } } else if(j==2||j==6) { if(tnumb) { tnumb--; temp++; } else { ok=1; break; } } else if(j==3||j==5) { if(tnumc) { tnumc--; temp++; } else { ok=1; break; } } } if(ok==1) { ans=max(ans,temp); continue; } ll t1=tnuma/3; ll t2=tnumb/2; ll t3=tnumc/2; ll ci=min(t1,min(t2,t3)); tnuma=tnuma-ci*3; tnumb=tnumb-ci*2; tnumc=tnumc-ci*2; temp=temp+ci*7; for (int j=1;j<=7;j++) { if((j==1||j==4||j==7)) { if(tnuma) { tnuma--; temp++; } else { break; } } else if(j==2||j==6) { if(tnumb) { tnumb--; temp++; } else { break; } } else if(j==3||j==5) { if(tnumc) { tnumc--; temp++; } else { break; } } } ans=max(ans,temp); // printf("i=%d ans=%lld\n",i,ans); } printf("%lld\n",ans); return 0; }
D. Walking Robot
Greedy, note that the maximum number of batteries in this question is storage capacity, can not exceed.
When s[i]=0, the batteries are preferred.
When s[i]=1, if the battery i s full at this time, the battery should be used first, and then the battery should be used. Note that the battery + 1 can not exceed its capacity.
The code is as follows:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF=0x3f3f3f3f; const int maxn=2*1e6+5; int n,a,b; int s[maxn]; int main() { scanf("%d%d%d",&n,&b,&a); int nb=b,na=a; for (int i=1;i<=n;i++) { scanf("%d",&s[i]); } int ans=0; for (int i=1;i<=n;i++) { if(s[i]==0) { if(a==na) { ans++; na--; } else if(na>0) { ans++; na--; } else { if(nb>0) { ans++; nb--; } else { break; } } } else { if(a==na) { ans++; na--; } else if(nb>0) { nb--; na++; ans++; } else { if(na>0) { na--; ans++; } else { break; } } } } printf("%d\n",ans); return 0; }
E. Two Teams
Set up a two-way linked list to solve this problem.
The code is as follows:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF=0x3f3f3f3f; const int maxn=2*1e5+5; int n,k; int loc[maxn]; int vis[maxn]; int ans[maxn]; struct List { int next; int pre; int data; }; List l [maxn]; int main() { scanf("%d%d",&n,&k); memset (vis,0,sizeof(vis)); memset (l,-1,sizeof(l)); for (int i=1;i<=n;i++) { int x; scanf("%d",&x); loc[x]=i; l[i].data=x; if(i==1) { l[i].pre=-1; continue; } l[i].pre=i-1; l[i-1].next=i; } l[n].next=-1; int num=0; int kt=n; int ci=0; while(num<n) { ci++; int an; if(ci%2) an=1; else an=2; while(vis[kt]) kt--; int pos=loc[kt]; vis[kt]=1; ans[pos]=an; num++; int hpo=pos; int tpo=pos; int hp=-1,tp=-1; for (int i=0;i<k;i++) { if(l[hpo].pre==-1) { hp=-1; break; } hpo=l[hpo].pre; ans[hpo]=an; vis[l[hpo].data]=1; hp=hpo; num++; } for (int i=0;i<k;i++) { if(l[tpo].next==-1) { tp=-1; break; } tpo=l[tpo].next; ans[tpo]=an; vis[l[tpo].data]=1; tp=tpo; num++; } if(hp==-1&&tp==-1) break; if(hp!=-1&&tp!=-1) { if(l[tp].next!=-1) l[l[tp].next].pre=l[hp].pre; if(l[hp].pre!=-1) l[l[hp].pre].next=l[tp].next; } if(hp==-1) { if(l[tp].next!=-1) l[l[tp].next].pre=-1; } if(tp==-1) { if(l[hp].pre!=-1) l[l[hp].pre].next=-1; } //printf("num=%d\n",num); } for (int i=1;i<=n;i++) printf("%d",ans[i]); printf("\n"); return 0; }