【二分答案+贪心】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 ln
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个人, 每人都需要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}
bool ok(int m)
{
Left[] = w[]; Right[] = ;
for(int i = ; i < n; i++)
{
if(i%)
{
if(w[]-Left[i-]-w[i] >= )
{
Left[i] = w[i];
Right[i] = ;
}
else
{
Left[i] = w[]-Left[i-];
Right[i] = w[i]-Left[i];
}
}
else
{
if(m-w[]-Right[i-]-w[i] >= )
{
Left[i] = ;
Right[i] = w[i];
}
else
{
Right[i] = m-w[]-Right[i-];
Left[i] = w[i]-Right[i];
}
}
}
return Left[n-] == ;
}
如家大神的比自己的简单诶。自己以后得学会简化算法:
bool ok(int m)
{
Left[] = w[]; Right[] = ;
for(int i = ; i < n; i++)
{
if(i%)
{
Left[i] = min(w[]-Left[i-], w[i]);
Right[i] = w[i] - Left[i];
}
else
{
Right[i] = min(m-w[]-Right[i-], w[i]);
Left[i] = w[i]-Right[i];
}
}
return Left[n-] == ;
}
附代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring> using namespace std;
const int maxn = ;
int n, ans;
int w[maxn];
int Left[maxn], Right[maxn]; bool ok(int m)
{
Left[] = w[]; Right[] = ;
for(int i = ; i < n; i++)
{
if(i%)
{
Left[i] = min(w[]-Left[i-], w[i]);
Right[i] = w[i] - Left[i];
}
else
{
Right[i] = min(m-w[]-Right[i-], w[i]);
Left[i] = w[i]-Right[i];
}
}
return Left[n-] == ;
} int main()
{
while(scanf("%d", &n) && n)
{
ans = ;
for(int i = ; i < n; i++)
scanf("%d", &w[i]);
if(n == ) {printf("%d\n", w[]); continue;}
w[n] = w[];
int L = , R = ;
for(int i = ; i < n; i++)
{
L = max(L, w[i]+w[i+]);
R += w[i];
}
if(n%)
{
while(L < R)
{
int M = L + (R-L)/;
if(ok(M)) R = M;
else L = M+;
}
}
printf("%d\n", L);
}
return ;
}
【二分答案+贪心】UVa 1335 - Beijing Guards的更多相关文章
- uva 1335 - Beijing Guards(二分)
题目链接:uva 1335 - Beijing Guards 题目大意:有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个相邻的人拥有同一种礼物, ...
- UVa 1335 Beijing Guards (二分+贪心)
题意:n 个人成一个圈,每个人想要 ri 种不同的礼物,要求相邻两个人没有相同的,求最少需要多少礼物. 析:如果 n 是偶数,那么答案一定是相邻两个人的礼物总种数之和的最大值,那么如果是奇数,就没那么 ...
- UVA 1335 Beijing Guards(二分答案)
入口: https://cn.vjudge.net/problem/UVA-1335 [题意] 有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个 ...
- uva 1335 - Beijing Guards
竟然用二分,真是想不到: 偶数的情况很容易想到:不过奇数的就难了: 奇数的情况下,一个从后向前拿,一个从前向后拿的分配方法实在太妙了! 注: 白书上的代码有一点点错误 代码: #include< ...
- BZOJ_2196_[Usaco2011 Mar]Brownie Slicing_二分答案+贪心
BZOJ_2196_[Usaco2011 Mar]Brownie Slicing_二分答案+贪心 Description Bessie烘焙了一块巧克力蛋糕.这块蛋糕是由R*C(1 <= R,C ...
- 洛谷3933 Chtholly Nota Seniorious 二分答案+贪心
题目链接 题意 给你一个N*M的矩阵 (N,M <=2000) 把他分成两部分 使两部分的极差较大的一个最小 求这个最小值.然后分矩阵的要求是:每个部分内部的方块之间,可以通过上下左右相互到 ...
- 【二分答案+贪心】解决“最小值最大”问题(UVa 12124 - Assemble)
Problem A - Assemble Time limit: 2 seconds Recently your team noticed that the computer you use to p ...
- 【洛谷】【二分答案+贪心】P1316 丢瓶盖
[题目描述:] 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? [ ...
- BZOJ5321 JXOI2017加法(二分答案+贪心+堆+树状数组)
二分答案后得到每个位置需要被加的次数.考虑贪心.从左到右考虑每个位置,将以该位置为左端点的区间按右端点从大到小加进堆.看该位置还需要被加多少次,如果不需要加了就不管,否则取堆顶区间将其选择,BIT实现 ...
随机推荐
- Convert to Objective-C ARC
今天在进行代码走查时,竟然发现了下面这段代码: Bad Code 顿时感觉吐槽无力,虽然我反复强调内存管理问题,无非就是谁申请谁释放,利用强弱引用避免 retain-cycles,但是还是会有这样那样 ...
- Tomcat 7 Connector 精读(1)
这个类图是本人截取的最重要的类的方法和属性. 其中ProtocalHandler是协议处理器,tomcat支持的协议以下方法可以看到.不同协议实现了不同的ProtocalHandler类. publi ...
- bzoj 3197 [Sdoi2013]assassin(Hash+DP+KM)
Description Input Output Sample Input 4 1 2 2 3 3 4 0 0 1 1 1 0 0 0 Sample Output 1 HINT [思路] Hash,D ...
- MFC 文件操作
MFC中文件的建立 在操作系统中,文件是放在一定的目录下,在创建以及操作文件以前,我们要查看文件要保存的目录有没有存在,如果不存在要创建.这就要用到GetFileAttributes()和Create ...
- MATLAB绘图与图形处理
参考:http://www.cnblogs.com/djcsch2001/tag/MATLAB/ matlab部分写的不错! 7.2 三维图形 7.2.1 三维曲线.面填色命令 命令1 com ...
- Storm因机器断电等,启动supervisor异常
Storm因机器断电等,启动supervisor错误 因机器断电或其他异常导致的supervisor意外终止,再次启动时报错: 2014-08-13 10:36:03 b.s.event [ERROR ...
- linux命令getopts
一.getopts 简介 由于shell命令行的灵活性,自己编写代码判断时,复杂度会比较高.使用内部命令 getopts 可以很方便地处理命令行参数.一般格式为: getopts options va ...
- Linux web工程部署远程必备软件安装
一.序 最近在将程序往linux上面部署,特此记录下部署步骤,待以后参考. web工程部署必备软件为:JDK.tomcat.数据库软件(oracle或mysql),远程监控.上传下载必备软件:VNC. ...
- vs开发工具使用问题
1.快捷键失效:(如:VS中CTRL+E,CTRL+D自动缩进功能不能使用) 通过设置“工具”-“选项”-“环境”-“键盘”-“应用以下其他键盘映射方案”中,选择"Visual C# 200 ...
- C#操作符的重载
操作符也是可以重载的,先总结一下操作符的性质: 如我们所知,操作符在不同的情况下有不同的语义,具体取决于它所操作的类型.例如,操作符“+”在操作数值类型的时候意味着“加”,在操作字符串时意味着“连接” ...