LA3177 Beijing Guards
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]
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
inline void swap(int &a, int &b)
{
int tmp = a;a = b;b = tmp;
}
inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '') c = ch, ch = getchar();
while(ch <= '' && ch >= '') x = x * + ch - '', ch = getchar();
if(c == '-') x = -x;
} const int INF = 0x3f3f3f3f;
const int MAXN = + ; int r[MAXN], n, ans = , left[MAXN], right[MAXN]; bool solve(int x)
{
left[] = r[];right[] = ;
for(register int i = ;i <= n;++ i)
{
if(i & )
{
right[i] = min(x - r[] - right[i - ], r[i]);
left[i] = r[i] - right[i];
}
else
{
left[i] = min(r[] - left[i - ], r[i]);
right[i] = r[i] - left[i];
}
}
return left[n] == ;
} int main()
{
while(scanf("%d", &n) != EOF && n)
{
for(register int i = ;i <= n;++ i) read(r[i]);
if(n == )
{
printf("%d\n", r[]);
continue;
}
ans = r[] + r[n];
for(register int i = ;i <= n;++ i) ans = max(ans, r[i] + r[i - ]);
if(n & )
{
int l = ans, r = ans, mid;
for(register int i = ;i <= n;++ i) r = max(r, ::r[i] * );
while(l <= r)
{
mid = (l + r) >> ;
if(solve(mid)) r = mid - , ans = mid;
else l = mid + ;
}
}
printf("%d\n", ans);
}
return ;
}
LA3177
LA3177 Beijing Guards的更多相关文章
- LA 3177 Beijing Guards(二分法 贪心)
Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...
- uva 1335 - Beijing Guards(二分)
题目链接:uva 1335 - Beijing Guards 题目大意:有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个相邻的人拥有同一种礼物, ...
- UVALive 3177 Beijing Guards
题目大意:给定一个环,每个人要得到Needi种物品,相邻的人之间不能得到相同的,问至少需要几种. 首先把n=1特判掉. 然后在n为偶数的时候,答案就是max(Needi+Needi+1)(包括(1,n ...
- 题解 UVA1335 【Beijing Guards】
UVA1335 Beijing Guards 双倍经验:P4409 [ZJOI2006]皇帝的烦恼 如果只是一条链,第一个护卫不与最后一个护卫相邻,那么直接贪心,找出最大的相邻数的和. 当变成环,贪心 ...
- 【二分答案+贪心】UVa 1335 - Beijing Guards
Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City ...
- uva 1335 - Beijing Guards
竟然用二分,真是想不到: 偶数的情况很容易想到:不过奇数的就难了: 奇数的情况下,一个从后向前拿,一个从前向后拿的分配方法实在太妙了! 注: 白书上的代码有一点点错误 代码: #include< ...
- Uva LA 3177 - Beijing Guards 贪心,特例分析,判断器+二分,记录区间内状态数目来染色 难度: 3
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- UVA 1335 Beijing Guards(二分答案)
入口: https://cn.vjudge.net/problem/UVA-1335 [题意] 有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个 ...
- Uva 长城守卫——1335 - Beijing Guards
二分查找+一定的技巧 #include<iostream> using namespace std; +; int n,r[maxn],Left[maxn],Right[maxn];//因 ...
随机推荐
- mybatis中处理结果集映射
单行结果集映射: 接口中方法返回值定义为Map类型,sql语句的resultType属性设置为map即可.这种情况默认把列名作为key,列中的值作为value. 也就是说用map<Strirng ...
- P3338 [ZJOI2014]力 /// FFT 公式转化翻转
题目大意: https://www.luogu.org/problemnew/show/P3338 题解 #include <bits/stdc++.h> #define N 300005 ...
- 调试存储过程与declare语句差异
当应用有调用存储过程,而节点有几十个或者上百个,找错是不是一个很麻烦的事情,这个时候,我建议写到数据库中,下面是我做的一个demo. 1. 建立错误日志记录表 drop table PUB_PROC ...
- 现代软件工程HW1:词频统计
作业详细要求:http://www.cnblogs.com/denghp83/p/8627840.html 基本功能 1. 统计文件的字符数(只需要统计Ascii码,汉字不用考虑,换行符不用考虑,'\ ...
- LeetCode 14.最长公共前缀(Python3)
题目: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow& ...
- 用 Docker 搭建Sha--dow--sock--s 笔记
首先得找一台海外服务器,该服务器一定要在海外. 一.在https://my.vultr.com/购买一台海外服务器,亲测选美国Miami速度最稳: 二.系统我选了CentOS 7 x64,在CentO ...
- centos6 nginx安装好以后,添加拓展ssl
前言 安装nginx的时候,只是执行最简单的安装,--user=nobody --group=nobody --prefix=/usr/local/nginx_1.8.1,没有安装http_ssl_m ...
- EF(Entity Framwork)结构
初次接触EF,看了一些资料,将自己对EF结构的理解记录如下: EF的核心是EDM----实体数据模型(.edmx).它由三部分组成:概念模型(.csdl文件).存储模型(.ssdl文件).映射规范(. ...
- WCF加密操作(包括证书和证书+帐号密码)
WCF作为.net三大组件之一,伟大之处不用多说,但是其加密配置对于我这样的萌新来说还是颇有难度,因此将几天来的研究成果共享出来,与各位共勉~ 首先声明我的开发环境,Win10创意者更新 + Visu ...
- VS2010-MFC(对话框:为控件添加消息处理函数)
转自:http://www.jizhuomi.com/software/156.html MFC为对话框和控件等定义了诸多消息,我们对它们操作时会触发消息,这些消息最终由消息处理函数处理.比如我们点击 ...