Beijing Guards
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.
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 iand 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.
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.
3 4 2 2 5 2 2 2 2 2 5 1 1 1 1 1 0
8 5 3
题目大意:有n个人围成一个圈,其中第i个人想要ri 个不同的礼物。相邻的两个人可以聊天,炫耀自己的礼物。如果两个相邻的人拥有同一种礼物,则双方都会很不高兴。问:一共需要多少种礼物才能满足所有人的需要?假设每种礼物有无穷多个,不相邻的两个人不会聊天,所以即使拿到相同的礼物也没关系。
比如,一共有5个人,每个人都要一个礼物,则至少需要3种礼物。如果把这3中礼物编号为1,2,3,则5个人拿到的礼物应分别是:1,2,1,2,3.如果每个人要两个礼物,则至少要5种礼物,且5个人拿到的礼物集合应该是:{1,2},{3,4},{1,5},{2,3},{4,5}。
分析:如果n为偶数,那么答案为相邻的两个人的r值之和的最大值,即p=max{ri+ri+1}(i=1,2,...,n),规定rn+1 = r1 。不难看出,这个数值是答案的下限,而且还可以构造出只用p种礼物的方案:对于编号为i的人,如果i是奇数,从前边开始取;如果i是偶数,从后边开始取。
n为奇数的情况,需要二分答案,假设已知共有p种礼物,设第1个人的礼物是1~r1,不难发现最优的分配策略一定是这样的:编号为偶数的人尽量往前取,编号为奇数的人尽量往后取。这样编号为n的人在不冲突的前提下,尽可能的往后取了rn样东西,最后判定编号为1的人和编号为n的人是否冲突即可。比如,n=5,A={2,2,5,2,5},p=8时,则第1个人取{1,2},第2个人取{3,4},第3个人取{8,7,6,5,2},第4个人取{1,3},第5个人取{8,7,6,5,4},由于第1个人与第5个人不冲突,所以p=8是可行的。
代码如下:
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 5 const int maxn = 100000 + 10; 6 int n, r[maxn], left[maxn], right[maxn]; 7 8 // 测试p个礼物是否足够。 9 // left[i]是第i个人拿到的“左边的礼物”总数,right类似 10 bool test(int p) { 11 int x = r[1], y = p - r[1]; 12 left[1] = x; right[1] = 0; 13 for(int i = 2; i <= n; i++) { 14 if(i % 2 == 0) { 15 right[i] = min(y - right[i-1], r[i]); // 尽量拿右边的礼物 16 left[i] = r[i] - right[i]; 17 } 18 else { 19 left[i] = min(x - left[i-1], r[i]); // 尽量拿左边的礼物 20 right[i] = r[i] - left[i]; 21 } 22 } 23 return left[n] == 0; 24 } 25 26 int main() { 27 int n; 28 while(scanf("%d", &n) == 1 && n) { 29 for(int i = 1; i <= n; i++) scanf("%d", &r[i]); 30 r[n+1] = r[1]; 31 32 int L = 0, R = 0; 33 for(int i = 1; i <= n; i++) L = max(L, r[i] + r[i+1]); 34 if(n % 2 == 1) { 35 for(int i = 1; i <= n; i++) R = max(R, r[i]*3); 36 while(L < R) { 37 int M = L + (R-L)/2; 38 if(test(M)) R = M; else L = M+1; 39 } 40 } 41 printf("%d\n", L); 42 } 43 return 0; 44 }