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 l ≤ 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. 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为偶数时可行,但n为奇数不可以(如:n = 5时,r = 2 2 2 2 2)
二分最终答案x不妨令第一个取1,2....r[1] - 1,r[1]
x被分为前r[1]个和后x - r[1]个,简称为前面和后面
设left[i]表示第i个人在前面取了left[i]个
righe[i]表示第i个人在后面取了right[i]个
当且仅当存在一种取法使得left[n] = 0时可行
我们只需要知道多少个,至于怎么取的我们不关心
不难发现,要使left[n]尽可能小,需要让right[n - 1]尽可能大,left[n - 2]尽可能小。。。
即:i为奇数时,令left[i]尽可能小;i为偶数时,令right[i]尽可能小
不难发现,当x >= max(r[i], r[i] + 1)时,满足如下转移方程
i为奇数:left[i] = min(r[1] - left[i - 1] ,r[i]), right[i] = r[i] - left[i]
i为偶数:right[i] = min(x - r[1] - right[i - 1], r[i]), left[i] = r[i] - right[i]
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #define min(a, b) ((a) < (b) ? (a) : (b)) 9 #define max(a, b) ((a) > (b) ? (a) : (b)) 10 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a)) 11 inline void swap(int &a, int &b) 12 { 13 int tmp = a;a = b;b = tmp; 14 } 15 inline void read(int &x) 16 { 17 x = 0;char ch = getchar(), c = ch; 18 while(ch < '0' || ch > '9') c = ch, ch = getchar(); 19 while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar(); 20 if(c == '-') x = -x; 21 } 22 23 const int INF = 0x3f3f3f3f; 24 const int MAXN = 100000 + 10; 25 26 int r[MAXN], n, ans = 0, left[MAXN], right[MAXN]; 27 28 bool solve(int x) 29 { 30 left[1] = r[1];right[1] = 0; 31 for(register int i = 2;i <= n;++ i) 32 { 33 if(i & 1) 34 { 35 right[i] = min(x - r[1] - right[i - 1], r[i]); 36 left[i] = r[i] - right[i]; 37 } 38 else 39 { 40 left[i] = min(r[1] - left[i - 1], r[i]); 41 right[i] = r[i] - left[i]; 42 } 43 } 44 return left[n] == 0; 45 } 46 47 int main() 48 { 49 while(scanf("%d", &n) != EOF && n) 50 { 51 for(register int i = 1;i <= n;++ i) read(r[i]); 52 if(n == 1) 53 { 54 printf("%d ", r[1]); 55 continue; 56 } 57 ans = r[1] + r[n]; 58 for(register int i = 2;i <= n;++ i) ans = max(ans, r[i] + r[i - 1]); 59 if(n & 1) 60 { 61 int l = ans, r = ans, mid; 62 for(register int i = 1;i <= n;++ i) r = max(r, ::r[i] * 3); 63 while(l <= r) 64 { 65 mid = (l + r) >> 1; 66 if(solve(mid)) r = mid - 1, ans = mid; 67 else l = mid + 1; 68 } 69 } 70 printf("%d ", ans); 71 } 72 return 0; 73 }