【二分答案+贪心】UVa 1335 - 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.

 

Input 

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

2

5

1

1

1

1

1

0

 

Sample Output 

8

5

3

题意: 有n个人, 每人都需要wi种礼物, 但是相邻的人得礼物是不可以相同的, 现在要你求出最少的礼物数满足全部人的需求, 每种礼物的数量是无限的.

 

解题思路:

       1. 当n为偶数时, 答案是相邻两人之间的的w值最大和, 即p = max{ p, wi + wi+1 }.并且可以看出,此p为礼物数的下限。

       2. 当n为奇数时, 采用二分答案求解左边界为相邻两人之间wi值最大和(注意奇数情况时,第1人和第n人会导致冲突),右边界为所有人的wi值之和;假设p种礼物是否满足分配:

       假设: 第一个人得到礼物1~r1, 分配的贪心策略可以是: 偶数编号的人尽量往前取, 奇数的人尽量往后取. 这样需要第n个人在不冲突的条件下, 尽量可能的往后取wn样礼物. 最后判断编号1和编号n的人是否冲突即可.

             例: p = 8, r = {2,2,5,2,5}

             第一人:{1,2}, 第二人:{3,4}, 第三人:{8,7,6,5,2}, 第四人:{1,3}, 第五人:{8,7,6,5,4}

 1 bool ok(int m)

 2 {

 3     Left[0] = w[0]; Right[0] = 0;

 4     for(int i = 1; i < n; i++)

 5     {

 6         if(i%2)

 7         {

 8             if(w[0]-Left[i-1]-w[i] >= 0)

 9             {

10                 Left[i] = w[i];

11                 Right[i] = 0;

12             }

13             else

14             {

15                 Left[i] = w[0]-Left[i-1];

16                 Right[i] = w[i]-Left[i];

17             }

18         }

19         else

20         {

21             if(m-w[0]-Right[i-1]-w[i] >= 0)

22             {

23                 Left[i] = 0;

24                 Right[i] = w[i];

25             }

26             else

27             {

28                 Right[i] = m-w[0]-Right[i-1];

29                 Left[i] = w[i]-Right[i];

30             }

31         }

32     }

33     return Left[n-1] == 0;

34 }

如家大神的比自己的简单诶。自己以后得学会简化算法:

bool ok(int m)

{

    Left[0] = w[0]; Right[0] = 0;

    for(int i = 1; i < n; i++)

    {

        if(i%2)

        {

            Left[i] = min(w[0]-Left[i-1], w[i]);

            Right[i] = w[i] - Left[i];

        }

        else

        {

            Right[i] = min(m-w[0]-Right[i-1], w[i]);

            Left[i] = w[i]-Right[i];

        }

    }

    return Left[n-1] == 0;

}

附代码:

【二分答案+贪心】UVa 1335 - Beijing Guards
 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstdlib>

 4 #include<cstring>

 5 

 6 using namespace std;

 7 const int maxn = 100010;

 8 int n, ans;

 9 int w[maxn];

10 int Left[maxn], Right[maxn];

11 

12 bool ok(int m)

13 {

14     Left[0] = w[0]; Right[0] = 0;

15     for(int i = 1; i < n; i++)

16     {

17         if(i%2)

18         {

19             Left[i] = min(w[0]-Left[i-1], w[i]);

20             Right[i] = w[i] - Left[i];

21         }

22         else

23         {

24             Right[i] = min(m-w[0]-Right[i-1], w[i]);

25             Left[i] = w[i]-Right[i];

26         }

27     }

28     return Left[n-1] == 0;

29 }

30 

31 int main()

32 {

33     while(scanf("%d", &n) && n)

34     {

35         ans = 0;

36         for(int i = 0; i < n; i++)

37             scanf("%d", &w[i]);

38         if(n == 1) {printf("%d\n", w[0]); continue;}

39         w[n] = w[0];

40         int L = 0, R = 0;

41         for(int i = 0; i < n; i++)

42         {

43             L = max(L, w[i]+w[i+1]);

44             R += w[i];

45         }

46         if(n%2)

47         {

48             while(L < R)

49             {

50                 int M = L + (R-L)/2;

51                 if(ok(M)) R = M;

52                 else L = M+1;

53             }

54         }

55         printf("%d\n", L);

56     }

57     return 0;

58 }
View Code

 

你可能感兴趣的:(uva)