UVA 1335 Beijing Guards

1335 - Beijing Guards

Time limit: 3.000 seconds

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

Sample Input

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

Sample Output

   
   
   
   
8
5
3
题意:
n个士兵围成一圈,要给他们发奖品,第 i 个士兵发 ri 个奖品,且相邻两个士兵的奖品不能有相同的,求最少的奖品种类数。
分析:
n为偶数时,答案为相邻两个士兵的 r 值之和的最大值;
n为奇数时需要二分求解,假设共有 p 种奖品,令第一个人的奖品为1 - r1,则最优分配策略为:编号为偶数的人尽量往前取,编号为奇数的人尽量往后取。
这样,编号为 n 的人在不冲突的前提下,尽可能往后取了 rn 样奖品,最后判定编号为1和编号为n的人是否冲突即可。
例如:
n = 5,r[5] = {2, 2, 5, 2, 5},则这5个士兵对应奖品编号为{1, 2}, {3, 4}, {8, 7, 6, 5, 2}, {1, 3}, {8, 7, 6, 5, 4}.由于第1个人和第5个人不冲突
所以8种奖品满足
代码如下:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 100000 + 10;
int r[maxn], Left[maxn], //Left[i]:第i个人在1 - r1范围内(左边)取了几个
Right[maxn];   //Right[i]:第i个人在r1+1 - p范围内取了几个
int n;
bool test(int p){
    int x = r[1], y = p - r[1];
    Left[1] = x; Right[1] = 0;
    for(int i = 2; i <= n; i++){
        if(i % 2 == 1){
            Right[i] = min(y - Right[i - 1], r[i]);  //尽量拿左边的,够拿则拿r[i]个,不够则拿最多能拿的
            Left[i] = r[i] - Right[i];
        }
        else{
            Left[i] = min(x - Left[i - 1], r[i]);
            Right[i] = r[i] - Left[i];
        }
    }
    return Left[n] == 0;  //第n个人没拿左边的,不冲突,可行
}
int main(){
    while(scanf("%d", &n) == 1 && n){
        for(int i = 1; i <= n; i++) scanf("%d", &r[i]);
        if(n == 1){printf("%d\n", r[1]); continue;}     //必须特判n=1
        r[n + 1] = r[1];
        int L = 0;
        for(int i = 1; i <= n; i++) L = max(L, r[i] + r[i + 1]);  //L:n为偶数时的最优值
        if(n % 2 == 1){
            int R = 0;
            for(int i = 1; i <= n; i++) R = max(R, 3 * r[i]);   //R:n为奇数时的上限
            while(L < R){                 //二分查找最优值
                int M = L + (R - L) / 2;
                if(test(M)) R = M;
                else L = M + 1;
            }
        }
        printf("%d\n", L);
    }
    return 0;
}




你可能感兴趣的:(UVA 1335 Beijing Guards)