Uva 1335 Beijing Guards 解题报告(策略+二分)
2014-07-15 15:41
459 查看
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.
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.
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.
解题报告:《训练指南》例题。
策略很难想。偶数的很容易相出来,奇数的真的有难度了。
然后二分倒没什么。代码如下:
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 ln
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.
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 typesof 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
3 4 2 2 5 2 2 2 2 2 5 1 1 1 1 1 0
Sample Output
8 5 3
解题报告:《训练指南》例题。
策略很难想。偶数的很容易相出来,奇数的真的有难度了。
然后二分倒没什么。代码如下:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <map> #include <string> using namespace std; #define ff(i, n) for(int i=0;i<(n);i++) #define fff(i, n, m) for(int i=(n);i<=(m);i++) #define dff(i, n, m) for(int i=(n);i>=(m);i--) typedef long long LL; typedef unsigned long long ULL; void work(); int main() { #ifdef ACM freopen("in.txt", "r", stdin); #endif // ACM work(); } /////////////////////////////////////////// int n; int need[111111]; bool check(int p) { int l=need[1], r=p-l; int left=l, right=0; fff(i, 2, n) { if(i%2==0) { left=min(l-left, need[i]); right=max(need[i]-left, 0); } else { right=min(r-right, need[i]); left=max(need[i]-right, 0); } } return left==0; } void work() { while(~scanf("%d", &n) && n) { fff(i, 1, n) scanf("%d", need+i); if(n==1) { printf("%d\n", need[1]); continue; } need[n+1] = need[1]; int l = 0; fff(i, 1, n) l=max(l, need[i]+need[i+1]); if(n&1) { int r = 200000; while(l<=r) { int mid = (l+r)/2; if(check(mid)) r=mid-1; else l=mid+1; } } printf("%d\n", l); } }
相关文章推荐
- UVA 1335 Beijing Guards 二分贪心
- 二分+贪心-uva-1335 - Beijing Guards
- UVa 1450 Airport 解题报告(二分+策略)
- UVa 1335 Beijing Guards (二分+贪心)
- CERC 2004 / UVa 1335 Beijing Guards (二分&贪心&想法题)
- uva 1335 - Beijing Guards(二分)
- uva 1335 Beijing Guards(二分)
- uva 1335 - Beijing Guards(二分)
- UVALive 3177 && uva 1335 Beijing Guards (贪心+二分)
- UVA 1335 Beijing Guards(二分+构造)
- uva 1335 - Beijing Guards(二分)
- UVA 1335 Beijing Guards 二分贪心
- UVa 1422 Processor 解题报告(二分)
- UVA 1335 - Beijing Guards(贪心)
- UVA 11478 查分约束+二分答案 解题报告
- ACM: uva 1335 - Beijing Guards
- UVALive - 3177 Beijing Guards 二分
- Beijing Guards UVALive - 3177 (二分)
- Uva 1350 - Pinary 解题报告(斐波那契+二分)
- Uva 12124 Assemble 解题报告(二分)