题意:n 个人成一个圈,每个人想要 ri 种不同的礼物,要求相邻两个人没有相同的,求最少需要多少礼物。

析:如果 n 是偶数,那么答案一定是相邻两个人的礼物总种数之和的最大值,那么如果是奇数,就没那么好做了,我们可以二分答案,

在每次判定时,我们可以有这样的贪心策略,第一个人 1 - r1,在后面的人中,编号为奇数的尽量往后取,编号为偶数的尽量往前取,

因为这样我们才能保证第 n 个人和第一个人尽量不相交。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <unordered_map>
  17. #include <unordered_set>
  18. #define debug() puts("++++");
  19. #define freopenr freopen("in.txt", "r", stdin)
  20. #define freopenw freopen("out.txt", "w", stdout)
  21. using namespace std;
  22.  
  23. typedef long long LL;
  24. typedef pair<int, int> P;
  25. const int INF = 0x3f3f3f3f;
  26. const double inf = 0x3f3f3f3f3f3f;
  27. const double PI = acos(-1.0);
  28. const double eps = 1e-8;
  29. const int maxn = 1e5 + 5;
  30. const int mod = 2000;
  31. const int dr[] = {-1, 1, 0, 0};
  32. const int dc[] = {0, 0, 1, -1};
  33. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  34. int n, m;
  35. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  36. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  37. inline bool is_in(int r, int c){
  38. return r >= 0 && r < n && c >= 0 && c < m;
  39. }
  40.  
  41. int a[maxn];
  42. int l[maxn], r[maxn];
  43.  
  44. bool judge(int mid){
  45. l[1] = a[1], r[1] = 0;
  46. int x = a[1], y = mid - a[1];
  47. for(int i = 2; i <= n; ++i){
  48. if(i & 1){
  49. r[i] = min(a[i], y-r[i-1]);
  50. l[i] = a[i] - r[i];
  51. if(l[i] + l[i-1] > x) return false;
  52. }
  53. else{
  54. l[i] = min(a[i], x-l[i-1]);
  55. r[i] = a[i] - l[i];
  56. if(r[i] + r[i-1] > y) return false;
  57. }
  58. }
  59. return l[n] == 0;
  60. }
  61.  
  62. int solve(){
  63. if(n & 1){
  64. int l = 1, r = 5e5;
  65. while(l < r){
  66. int mid = (r + l) >> 1;
  67. if(judge(mid)) r = mid;
  68. else l = mid + 1;
  69. }
  70. return l;
  71. }
  72. int ans = 0;
  73. for(int i = 1; i <= n; ++i) ans = max(ans, a[i] + a[i+1]);
  74. return ans;
  75. }
  76.  
  77. int main(){
  78. while(scanf("%d", &n) == 1 && n){
  79. for(int i = 1; i <= n; ++i) scanf("%d", a+i);
  80. if(1 == n){ printf("%d\n", a[1]); continue; }
  81. a[n+1] = a[1];
  82. printf("%d\n", solve());
  83. }
  84. return 0;
  85. }
  86. /*
  87. 9
  88. 8
  89. 15
  90. 35
  91. 16
  92. 21
  93. 90
  94. 55
  95. 50
  96. 32
  97. */

UVa 1335 Beijing Guards (二分+贪心)的更多相关文章

  1. uva 1335 - Beijing Guards(二分)

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

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

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

  3. UVA 1335 Beijing Guards(二分答案)

    入口: https://cn.vjudge.net/problem/UVA-1335 [题意] 有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个 ...

  4. uva 1335 - Beijing Guards

    竟然用二分,真是想不到: 偶数的情况很容易想到:不过奇数的就难了: 奇数的情况下,一个从后向前拿,一个从前向后拿的分配方法实在太妙了! 注: 白书上的代码有一点点错误 代码: #include< ...

  5. Uva 长城守卫——1335 - Beijing Guards

    二分查找+一定的技巧 #include<iostream> using namespace std; +; int n,r[maxn],Left[maxn],Right[maxn];//因 ...

  6. UVA 1149 Bin Packing 二分+贪心

    A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...

  7. LA 3177 Beijing Guards(二分法 贪心)

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

  8. LA3177 Beijing Guards

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

  9. 题解 UVA1335 【Beijing Guards】

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

随机推荐

  1. poj 2151Check the difficulty of problems<概率DP>

    链接:http://poj.org/problem?id=2151 题意:一场比赛有 T 支队伍,共 M 道题, 给出每支队伍能解出各题的概率~  求 :冠军至少做出 N 题且每队至少做出一题的概率~ ...

  2. 九度OJ 1170:找最小数 (最值)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6451 解决:2843 题目描述: 第一行输入一个数n,1 <= n <= 1000,下面输入n行数据,每一行有两个数,分别是x ...

  3. 九度OJ 1059:abc (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3642 解决:2869 题目描述: 设a.b.c均是0到9之间的数字,abc.bcc是两个三位数,且有:abc+bcc=532.求满足条件的 ...

  4. AWS:5.公有云编程

    主要内容 1.AWS接口 2.使用AWS命令行 3.使用python sdk编程 AWS接口 Console:web控制台 登录amazon后在"我的账户" -> AWS管理 ...

  5. going

  6. CMMI过程改进反例

     近期一直在看CMMI的资料,越看认为越有意思.今天看到过程改进的时候,突然想起来之前所在的公司发生的过程改进相关的事儿来. 公司通过CMMI3级认证之后.PMO部门经理(公司还有质量管理部门经理 ...

  7. 不使用 spring-boot-starter-parent 构建 spring boot 应用

    创建 spring-boot 应用通用方法是配置 pom.xml,定义 为 spring-boot-start-parent.如下: <parent> <groupId>org ...

  8. Form表单插件

    jQuery Form是一个优秀的表单插件,它可以非常容易地,无侵入地升级HTML表单以支持Ajax jQuery Form表单插件的下载地址为 http://jquery.malsup.com/fo ...

  9. 【转】如何让虚拟目录里面的webconfig不继承网站的设置

    [转]http://www.cnblogs.com/Sue_/articles/2037556.html 必須在上一层虚拟目录(如根目录)所在的Web.config加上 如:<location ...

  10. 一次跨域请求出现 OPTIONS 请求的问题及解决方法

    问题背景浏览器从一个域名的网页去请求另一个域名的资源时,域名.端口.协议任一不同,都是跨域 在前后端开发过程经常会遇到跨域问题.网上也都有解决方案. 写这篇文章时,我们碰到的一个场景是:要给s系统做一 ...