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
#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;}
- Beijing Guards
- Beijing Guards
- Beijing Guards
- Beijing Guards LA3177
- uva 1335 Beijing Guards
- uva 1335 - Beijing Guards
- LA 3177 Beijing Guards
- 1335 - Beijing Guards
- UVA - 1335 Beijing Guards
- UVA 1335 - Beijing Guards
- UVA 1335 Beijing Guards
- UVA1335-- Beijing Guards
- uva 1335Beijing Guards
- UVALive - 3177 Beijing Guards
- UVALive 3177 Beijing Guards
- uva1335 - Beijing Guards
- UVA 1335 Beijing Guards
- UVALIVE 3177 Beijing Guards
- 8种垂直居中的方法
- HDU_1712 ACboy needs your help(DP)
- hdu 2602 01背包深入优化
- Unity3D之GUI基础以及常用GUI控件使用
- 2015/02/08
- Beijing Guards
- mysql常用命令大全(百度文库下载的,备忘一下)
- Mac快捷键
- Android中UI线程与后台线程交互设计
- Java Mac Eclipse Console 显示全部的输出
- C++对象模型-笔记一
- Java笔记二十一.深入解析I/O编程之节点流
- 没长一张萝莉的脸不要紧,可是你敢不敢有颗爷们的心
- Redis与Memcached的区别