首页 > 代码库 > UVALIVE 3177 Beijing Guards
UVALIVE 3177 Beijing Guards
Description
Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City Wall, the Inner City Wall, and finally the Outer City Wall. Most of these walls were demolished in the 50s and 60s to make way for roads. The walls were 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 should 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 many 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 integer ln100000, the number of guard towers. The next n lines correspond to the n guards: each line contains an integer, the number of awards the guard requires. Each guard requires at least 1, and at most l00000 awards. Guard i and i + 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 block with n = 0.
Output
For each test case, you have to output a line containing a single integer, the minimum number x of award types that allows us to motivate the guards. That is, if we have x types of awards, then we can give as many 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
题意就不用说了
解法:
如果n是偶数,那么就直接是相邻两个和的最大值,奇数的情况就很复杂了,不过还是可以二分答案,然后判断可行性,第一个放1——a[1],其他的偶数位尽量往编号小的放,奇数伟尽量放编号大的,但是怎么处理呢!我们可以将答案ans看作 1——ans这个区间,对于每个数统计放在1——a[1]这个区间的个数,和a[1+1]——ans这个区间的个数,最后看看最后一个的左区间数是否为0;
#include<iostream>#include<cstdio>#include<cstdlib>#include<ctime>#include<cstring>#include<string>#include<map>#include<set>#include<cmath>#include<stack>#include<queue>#include<vector>#include<algorithm>#include<sstream>#define inf 0x3f3f3f3f#define PI acos(-1.0)#define eps 1e-6#define LL long long#define MEM(a,b) memset(a,b,sizeof(a))#define PB push_back#define MP make_pair#define IN freopen("in.txt","r",stdin);#define OUT freopen("out.txt","w",stdout);#define BUG printf("bug************bug************bug\n");using namespace std;const int maxn=100000+10;int a[maxn],n,l[maxn],r[maxn];bool if_find(int m){ l[1]=a[1]; r[1]=0; int LLL=a[1]; int RRR=m-a[1]; for (int i=2;i<=n;i++) { if (i%2==1) { r[i]=min(RRR-r[i-1],a[i]); l[i]=a[i]-r[i]; } else { l[i]=min(LLL-l[i-1],a[i]); r[i]=a[i]-l[i]; } } if (l[n]==0) return 1; else return 0;}int find(int x,int y){ while(x<y) { int m=x+(y-x)/2; if (if_find(m)) y=m; else x=m+1; } return x;}int main(){ int L,R,ans; while(scanf("%d",&n)!=EOF && n) { L=0; R=0; ans=0; for (int i=1;i<=n;i++) { scanf("%d",&a[i]); if (i>0)ans=max(ans,a[i]+a[i-1]); } ans=max(ans,a[1]+a[n]); if (n%2==0) {printf("%d\n",ans); continue; } if (n==1){printf("%d\n",a[1]); continue; } L=ans; R=3*ans; ans=find(L,R); printf("%d\n",ans); } return 0;}
UVALIVE 3177 Beijing Guards