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 iand 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个人围成一个圈,其中第i个人想要ri 个不同的礼物。相邻的两个人可以聊天,炫耀自己的礼物。如果两个相邻的人拥有同一种礼物,则双方都会很不高兴。问:一共需要多少种礼物才能满足所有人的需要?假设每种礼物有无穷多个,不相邻的两个人不会聊天,所以即使拿到相同的礼物也没关系。
  比如,一共有5个人,每个人都要一个礼物,则至少需要3种礼物。如果把这3中礼物编号为1,2,3,则5个人拿到的礼物应分别是:1,2,1,2,3.如果每个人要两个礼物,则至少要5种礼物,且5个人拿到的礼物集合应该是:{1,2},{3,4},{1,5},{2,3},{4,5}。 分析:如果n为偶数,那么答案为相邻的两个人的r值之和的最大值,即p=max{ri+ri+1}(i=1,2,...,n),规定rn+1 = r1 。不难看出,这个数值是答案的下限,而且还可以构造出只用p种礼物的方案:对于编号为i的人,如果i是奇数,从前边开始取;如果i是偶数,从后边开始取。
  n为奇数的情况,需要二分答案,假设已知共有p种礼物,设第1个人的礼物是1~r1,不难发现最优的分配策略一定是这样的:编号为偶数的人尽量往前取,编号为奇数的人尽量往后取。这样编号为n的人在不冲突的前提下,尽可能的往后取了rn样东西,最后判定编号为1的人和编号为n的人是否冲突即可。比如,n=5,A={2,2,5,2,5},p=8时,则第1个人取{1,2},第2个人取{3,4},第3个人取{8,7,6,5,2},第4个人取{1,3},第5个人取{8,7,6,5,4},由于第1个人与第5个人不冲突,所以p=8是可行的。 代码如下:
 #include<cstdio>
#include<algorithm>
using namespace std; const int maxn = + ;
int n, r[maxn], left[maxn], right[maxn]; // 测试p个礼物是否足够。
// left[i]是第i个人拿到的“左边的礼物”总数,right类似
bool test(int p) {
int x = r[], y = p - r[];
left[] = x; right[] = ;
for(int i = ; i <= n; i++) {
if(i % == ) {
right[i] = min(y - right[i-], r[i]); // 尽量拿右边的礼物
left[i] = r[i] - right[i];
}
else {
left[i] = min(x - left[i-], r[i]); // 尽量拿左边的礼物
right[i] = r[i] - left[i];
}
}
return left[n] == ;
} int main() {
int n;
while(scanf("%d", &n) == && n) {
for(int i = ; i <= n; i++) scanf("%d", &r[i]);
r[n+] = r[]; int L = , R = ;
for(int i = ; i <= n; i++) L = max(L, r[i] + r[i+]);
if(n % == ) {
for(int i = ; i <= n; i++) R = max(R, r[i]*);
while(L < R) {
int M = L + (R-L)/;
if(test(M)) R = M; else L = M+;
}
}
printf("%d\n", L);
}
return ;
}

 

LA 3177 Beijing Guards(二分法 贪心)的更多相关文章

  1. Uva LA 3177 - Beijing Guards 贪心,特例分析,判断器+二分,记录区间内状态数目来染色 难度: 3

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  2. UVALive 3177 Beijing Guards

    题目大意:给定一个环,每个人要得到Needi种物品,相邻的人之间不能得到相同的,问至少需要几种. 首先把n=1特判掉. 然后在n为偶数的时候,答案就是max(Needi+Needi+1)(包括(1,n ...

  3. UVa 1335 Beijing Guards (二分+贪心)

    题意:n 个人成一个圈,每个人想要 ri 种不同的礼物,要求相邻两个人没有相同的,求最少需要多少礼物. 析:如果 n 是偶数,那么答案一定是相邻两个人的礼物总种数之和的最大值,那么如果是奇数,就没那么 ...

  4. LA3177 Beijing Guards

    Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...

  5. 题解 UVA1335 【Beijing Guards】

    UVA1335 Beijing Guards 双倍经验:P4409 [ZJOI2006]皇帝的烦恼 如果只是一条链,第一个护卫不与最后一个护卫相邻,那么直接贪心,找出最大的相邻数的和. 当变成环,贪心 ...

  6. uva 1335 - Beijing Guards(二分)

    题目链接:uva 1335 - Beijing Guards 题目大意:有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个相邻的人拥有同一种礼物, ...

  7. 【二分答案+贪心】UVa 1335 - Beijing Guards

    Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City ...

  8. UVA-1335(UVALive-3177) Beijing Guards 贪心 二分

    题面 题意:有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个相邻的人拥有同一种礼物,则双方都会很不高兴,问最少需要多少种不同的礼物才能满足所有人 ...

  9. poj3122-Pie(二分法+贪心思想)

    一,题意: 有f+1个人(包括自己),n块披萨pie,给你每块pie的半径,要你公平的把尽可能多的pie分给每一个人 而且每个人得到的pie来自一个pie,不能拼凑,多余的边角丢掉.二,思路: 1,输 ...

随机推荐

  1. WCF入门到精通(二)——契约

    第一次接触WCF,如有写的不对的地方有望大家指出来,谢谢!! 本篇文章主要说下WCF中的契约的种类.契约的种类.如何定义契约等内容. 契约是一种双边或多边的协议,是利益相关方就某个问题达成的一种共识, ...

  2. 【Java基础】一个有意思的泛型方法Arrays.asList(T... a)

    总结 利用Arrays.asList方法返回的List是不允许add和remove的,这种list的长度不可变,因为底层依然是写数组. Arrays.asList的返回值是调用是传入T类型的List, ...

  3. linux进程,作业,守护进程,进程间同步

    ps axj命令查看系统中的进程.参数a表示不仅列当前用户的进程,也列出所有其他用户的进程,参数x表示不仅列有控制终端的进程,也列出所有无控制终端的进程,参数j表示列出与作业控制相关的信息: 凡是TP ...

  4. ehcache简单使用

    项目中需要实现一个功能,定时查询FTP服务器某个目录下的文件,并及时下载至本机,同时不能消耗太多系统资源. 最后实现是使用ehcache,将文件路径和文件大小缓存,如果前后两次无变化,则忽略.如果同一 ...

  5. nyoj 540 奇怪的排序

    奇怪的排序 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 最近,Dr. Kong 新设计一个机器人Bill.这台机器人很聪明,会做许多事情.惟独对自然数的理解与人类 ...

  6. JDBC与SQL SERVER各个版本的连接方法

    转至:blog.csdn.net/ying5420/article/details/4488246 1.SQL SERVER 2000 JDBC驱动程序:msbase.jar.mssqlserver. ...

  7. 使用tomcat的jndi方式连接mysql的字符编码设置

    最近新项目使用tomcat中配置jndi连接mysql的方式,在使用过程中发现查询条件为中文的时候查询不出结果,经过一通折腾,发现是jndi在连接数据库的时候忘记设置字符编码. 修改之后的完整配置如下 ...

  8. [USACO10MAR]伟大的奶牛聚集

    [USACO10MAR]伟大的奶牛聚集 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会. 每个奶牛居住在 N(1<=N& ...

  9. SAP生产订单状态

    SAP系统的常见订单状态如下: ·        CRTD (创建):标识生产订单刚刚创建,此时禁止做后续发料和报工确认等操作: ·        PREL (部分下达):当生产订单部分下达时,如仅下 ...

  10. 微信中QQ表情的解析(php)

    微信公众平台接受的消息中,标签是用'/:'开头的字符串表示的,假设要在网页上显示(比方制作微信大屏幕),就须要进行转换. 所以我向微信公众平台按顺序发送了各个QQ表情,在微信公众平台后台能够看到接受的 ...