Beijing Guards

来源:互联网 发布:知乎怎么匿名提问 编辑:程序博客网 时间:2024/05/31 13:16

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.


Sample Input

The input contains several blocks of test eases. Each case begins with a line containing a single integer 1<=n<=100000, 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.


Sample 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.


Input
3
4
2
2
5
2
2
2
2
2
5
1
1
1
1
1
0


Output
8
5
3

  我一看到这道题,第一个想法是枚举。但是人数最多有100000,如果r也是100000,那么数据太大了。而且枚举要考虑把所有人礼物的种类列出来,但是题目只要求我们输出最终的礼物种类数,用枚举显然不是好方法。思路我自己没有想出来,看了分析后又考虑了一会儿才想明白。我觉得这道题的方法可以应用于只需要结果而不注重过程具体如何实现的题目。下面我来分析一下:
   如果人数是偶数,如果我们让奇数号的人和偶数号的人拿到的礼物不一样,那么由于第一个和最后一个人的序号是一奇一偶拿的礼物自然不同,所以我们只要保证相邻的两个拿到不同的礼物就可以了,礼物的数量最小值正好等于相邻两个人r值之和的最大值。而人数是奇数时,由于第一个和最后一个人的序号都是奇数,就有可能冲突,我们只需最后判断一下二者是否冲突就可以了。我们需要寻找礼物的最小值,用二分查找。下限L是相邻r值的最大值,上限为r【i】*3的最大值(因为我们只需保证一个人与他左右的人拿到的礼物都不同就可以了,所以最坏的情况就是r【i】最大的乘3,如果这个人和他左右的人的礼物都不同,那么所有人的礼物肯定就都不一样了)。分礼物的时候,因为我们没有必要知道礼物具体是怎么分的,所以只需要记录取走的礼物数量就可以了(我们假设只要礼物种类足够,相邻两个人拿的礼物是不同的)。代码如下:
#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int maxn=100000+5;int aw[maxn],Left[maxn],Right[maxn];int n;bool check(int p){int x=aw[0],y=p-aw[0];Left[0]=x;Right[0]=0;for(int i=1;i<n;i++){if(i%2!=0){Left[i]=min(x-Left[i-1],aw[i]);Right[i]=aw[i]-Left[i];}if(i%2==0){Right[i]=min(y-Right[i-1],aw[i]);Left[i]=aw[i]-Right[i];}}return Left[n-1]==0;}int main(){while(scanf("%d",&n)==1&&n){for(int i=0;i<n;i++)scanf("%d",&aw[i]);if(n==1){printf("%d\n",aw[0]);continue;}int L=0,R=0;aw[n]=aw[0];for(int i=0;i<n;i++)L=max(aw[i]+aw[i+1],L);if(n%2!=0){for(int i=0;i<n;i++) R=max(R,aw[i]*3);while(L<R){int M=(R+L)/2;if(check(M)) R=M;else L=M+1; }}printf("%d\n",L);}return 0;}


0 0