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

最新文章

  1. 【Kindle】pdf转mobi适合kindle查看格式
  2. 用CSS3实现瀑布流布局
  3. 高性能滚动 scroll 及页面渲染优化
  4. gcc相关
  5. Powershell-入门
  6. Executing Raw SQL Queries using Entity Framework
  7. Ubuntu下安装Mysql并使用
  8. Qt学习之路(49): 通用算法
  9. jvm 加载class文件过程
  10. ASP.NET Core 一步步搭建个人网站(7)_Linux系统移植
  11. OpenResty和Resis一些基本的性能配置
  12. 简单说说Android自定义view学习推荐的方式
  13. SQL Server 中执行Shell脚本计算本地文件的内容大小
  14. sale.order
  15. MXNET:多层感知机
  16. Twisted 安装
  17. idea导入myeclipes项目、运行项目
  18. 修改urllib2源代码,定制User-Agent,一劳永逸
  19. 【bug】使用微信分享SDK,配置成功但分享信息异常
  20. 开源应用框架BitAdminCore:更新日志20180903

热门文章

  1. (转)Nginx+Php-fpm运行原理详解
  2. JS流程控制语句 来来回回(Do...while循环) 先执行后判断 do while结构的基本原理和while结构是基本相同的,但是它保证循环体至少被执行一次。
  3. MySQL中\g和\G的作用
  4. CF627A Xor Equation
  5. maven/gradle版本统一示例
  6. 【转帖】WebRTC回声抵消模块简要分析
  7. Redis学习目录
  8. 前端面试题之一JAVASCRIPT(算法类)
  9. 渗透测试入门DVWA 环境搭建
  10. Neo4j-Cypher语言语法