A greedy training guide is classic.
Beijing guards
Time limit:3000 Ms |
|
Memory limit:Unknown |
|
64bit Io format:% LLD & % LlU |
[Submit] [go
Back] [Status]
Description
Beijing was once surrounded by four rings of city Wals: The Forbidden City Wall, the Imperial City Wall, the inner city wall, and finally the outer city wall. most of these wallwere demolished in the 50 s
And 60 s to make way for roads. the wallwere protected by guard towers, and there was a guard living in each tower. the wall can be considered to be a large ring, where every guard tower has exaetly two neighbors.
The guard had to keep an eye on his section of the wall all day, so he had to stay in the tower. this is a very boring job, thus it is important to keep the guards motivated. the best way to motivate a guard
Is to give him lots of awards. there are several different types of awards that can be given: the Distinguished Service Award, the nicest uniform award, the master guard award, the superior eyesight Award, etc. the Central Department of City guards determined
How many awards have to be given to each of the guards. an award can be given to more than one guard. however, you have to pay attention to one thing: You shocould not give the same award to two neighbors, since a guard cannot be proud of his award if his neighbor
Already has this award. The task is to write a program that determines how does different types of awards are required to keep all the guards motivated.
Input
The input contains several blocks of test eases. Each case begins with a line containing a single integerLN100000,
The number of guard towers. The nextNLines correspond toNGuards: each line contains an integer, the number of awards the guard requires. Each guard requires at least 1, and at most
L00000 awards. GuardIAndI+ 1 are neighbors, they cannot receive the same award. The first guard and the last guard are also neighbors.
The input is terminated by a blockN= 0.
Output
For each test case, you have to output a line containing a single integer, the minimum numberXOf Award types that allows us to motivate the guards. That is, if we haveXTypes
Of awards, then we can give as login awards to each guard as he requires, and we can do it in such a way that the same type of award is not given to neighboring guards. A guard can receive only one award from each type.
Sample Input
34225222225111110
Sample output
853
#include<iostream>#include<cstring>#include<algorithm>#include<string>#include<cstdio>using namespace std;int a[100100];int p[100100],q[10010];int n,ans;bool jud(int m){ int x=a[1],y=m-a[1]; p[1]=x,q[1]=0; for(int i=2;i<=n;i++) { if(i%2==1) { q[i]=min(y-q[i-1],a[i]); p[i]=a[i]-q[i]; } else { p[i]=min(x-p[i-1],a[i]); q[i]=a[i]-p[i]; } } return p[n]==0;}int main(){ while(~scanf("%d",&n) && n) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); if(n==1) { printf("%d\n",a[1]); continue; } ans=0;a[n+1]=a[1]; for(int i=1;i<=n;i++) ans=max(ans,a[i]+a[i+1]); if( n%2) { int l=ans,r=0,m; for(int i=1;i<=n;i++) r=max(r,a[i]*3); while(l<r) { m=l+(r-l)/2; if(jud(m)) r=m; else l=m+1; } ans=l; } cout<<ans<<endl; } return 0;}