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 integerln100000, the number of guard towers. The next n lines correspond to then guards: each line contains an integer, the number of awards the guard requires. Each guard requires at least 1, and at most l00000 awards. Guardi 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 numberx of award types that allows us to motivate the guards. That is, if we havex 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.
题意:n个人为一个圈,第i人要ri个不同的礼物,相邻的人不能有相同的礼物,问满足所有人要求的最小礼物种类。如n=5,礼物编号1-5,{12,34,15,23,45}
思路:对n的奇偶情况分类解决,偶数的情况就是ri+ri+1的最大值,奇数的情况进行二分,用left和right记录取礼物情况
#include <iostream> using namespace std; #define MAX(a, b) ((a) > (b) ? a : b) #define MIN(a, b) ((a) < (b) ? a : b) const int maxn = 100010; int n, r[maxn], left1[maxn], right1[maxn]; //测试m个礼物是否足够 //left1[i]第i个人拿到左边礼物的总数 bool ok(int m) { int x = r[1], y = m - r[1]; left1[1] = x; right1[1] = 0; for (int i = 2; i <= n; i++) { if (i % 2 == 0) { left1[i] = MIN(x - left1[i - 1], r[i]); right1[i] = r[i] - left1[i]; } else { right1[i] = MIN(y - right1[i - 1], r[i]); left1[i] = r[i] - right1[i]; } } return left1[n] == 0; } int main() { while (cin>>n, n) { for (int i = 1; i <= n; i++) cin>>r[i]; if (n == 1) { printf("%d\n", r[1]); continue; } r[n + 1] = r[1]; int L = 0, R = 0; for (i = 1; i <= n; i++) L = MAX(L, r[i] + r[i + 1]);//下限 if (n % 2 == 1) { for (int i = 1; i <= n; i++) R = MAX(R, r[i] * 3); while (L < R) { int M = L + (R - L) / 2; if (ok(M)) R = M; else L = M + 1; } } printf("%d\n", L); } return 0; }