【二分答案+贪心】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;
}

附代码:

 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 }

时间: 2024-08-25 07:22:36

【二分答案+贪心】UVa 1335 - Beijing Guards的相关文章

uva 1335 Beijing Guards(二分)

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

[UVa 1335]Beijing Guards

题解 拿到题,没什么头绪,我们模拟一下,很容易得出一个结论: 如果$n$为偶数,那么答案就是$max(r[i]+r[i+1])$.具体方案就是,如果$i$为奇数,那么取$[1,r[i]]$:若$i$为偶数,则取$[ans-r[i]+1,ans]$. 同样我们此时的$ans$是答案的下界. 可当$n$为奇数时,我们这样贪心策略出现问题就是由于$1$和$n$都是奇数,会取$[1,r[1]]$,$[1,r[n]]$显然会重复,那么奇数我们就不能这样做了. 我们由$n$为偶数的思想,我们还是让$1$号取

Gym 100886J Sockets 二分答案 + 贪心

Description standard input/outputStatements Valera has only one electrical socket in his flat. He also has m devices which require electricity to work. He's got n plug multipliers to plug the devices, the i-th plug multiplier has ai sockets. A device

Luogu P1084 疫情控制 | 二分答案 贪心

题目链接 观察题目,答案明显具有单调性. 因为如果用$x$小时能够控制疫情,那么用$(x+1)$小时也一定能控制疫情. 由此想到二分答案,将问题转换为判断用$x$小时是否能控制疫情. 对于那些在$x$小时内不能够走到根节点的子节点上的军队,让他们尽量往上走即可,走到哪里是哪里,这样显然不会更劣. 对于那些在$x$小时内能走到根节点的子节点上的军队,就让他们先走到根节点的子节点上. 然后搞搞贪心即可. #include<iostream> #include<cstdio> #incl

[CSP-S模拟测试]:kill(二分答案+贪心)

题目传送门(内部题50) 输入格式 第一行包含四个整数$n,m,s$,表示人数.怪物数及任务交付点的位置.第二行包含$n$个整数$p_1,p_2,...,p_n$.第三行包含$n$个整数$q_1,q_2,...,q_n$. 输出格式 输出一行包含一个整数$ans$,表示答案. 样例 样例输入: 2 4 52 106 1 4 8 样例输出: 5 数据范围与提示 样例解释: 第一个人打位置为$4$的怪物,第二个人打位置为$8$的怪物,前者花$3$的时间,后者花$5$的时间,该方案对应的时间为$5$,

Educational Codeforces Round 3:D. Gadgets for dollars and pounds(二分答案+贪心)

看了菊苣的理解才知道怎么写..根本没思路啊一开始... 天数肯定在1~n之间,二分答案. 可以用保存前i天的最低美元和英镑汇率,没有规定哪天买,每天也没有购买数量限制,所以二分出一个答案mid的后,就在前mid天中汇率最低的时候一次性购入最便宜的就行了,judge一下总花费 打印答案的时候以ans为结果,再模拟一遍就好 #include"cstdio" #include"queue" #include"cmath" #include"s

BC 2015年百度之星程序设计大赛 - 初赛(1)(序列变换-二分答案贪心)

序列变换 Accepts: 816 Submissions: 3578 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description 给定序列A={A 1 ,A 2 ,...,A n } , 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:B i <B i+1 ,1≤i<N ). 我们定义从序列A到序列B变换的代价为cost(A,B

HDU - 5884 Sort (二分答案+贪心)

有n个数字,你需要把这n个数字合成一个数字,每次只能把k个数字合并成一个,花费为这k个数字的和. 给一个最大花费,问不超过这个最大花费的情况下,k的最小值. Sample Input 1 5 25 1 2 3 4 5 Sample Output 3 这个题很容易想到二分答案+优先队列check 然而这样复杂度是 O(n logn*logn ),会TLE(这特么都会TLE?加个读入优化就过了) 可以先给所有数字排个序,然后用两个队列,一个存原来的数字,一个存新合成的数字. 所以两个队列都是有序的.

Uva 长城守卫——1335 - Beijing Guards

二分查找+一定的技巧 1 #include<iostream> 2 using namespace std; 3 4 const int maxn=100000+10; 5 int n,r[maxn],Left[maxn],Right[maxn];//因为不用计算方案,所以可以按[1-r[i]]和[r[i]+1~p]中各拿几个分,当时没想到这个用set类写了个超耗时间的~~~~(>_<)~~~~ 6 7 bool ok(int p) 8 { 9 int x=r[1],y=p-r[1