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

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

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

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

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int a[maxn];
int l[maxn], r[maxn]; bool judge(int mid){
l[1] = a[1], r[1] = 0;
int x = a[1], y = mid - a[1];
for(int i = 2; i <= n; ++i){
if(i & 1){
r[i] = min(a[i], y-r[i-1]);
l[i] = a[i] - r[i];
if(l[i] + l[i-1] > x) return false;
}
else{
l[i] = min(a[i], x-l[i-1]);
r[i] = a[i] - l[i];
if(r[i] + r[i-1] > y) return false;
}
}
return l[n] == 0;
} int solve(){
if(n & 1){
int l = 1, r = 5e5;
while(l < r){
int mid = (r + l) >> 1;
if(judge(mid)) r = mid;
else l = mid + 1;
}
return l;
}
int ans = 0;
for(int i = 1; i <= n; ++i) ans = max(ans, a[i] + a[i+1]);
return ans;
} int main(){
while(scanf("%d", &n) == 1 && n){
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
if(1 == n){ printf("%d\n", a[1]); continue; }
a[n+1] = a[1];
printf("%d\n", solve());
}
return 0;
}
/*
9
8
15
35
16
21
90
55
50
32
*/

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. iOS 跳转到Appstore的链接及二维码

    1.应用内部跳转到Appstore 1.跳转到应用详情 [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"it ...

  2. Jeff Dean 排序时间计算

    Quicksort (sometimes called partition-exchange sort) https://en.m.wikipedia.org/wiki/Quicksort

  3. 性能测试--yslow

    YSlow YSlow可以对网站的页面进行分析,并告诉你为了提高网站性能,如何基于某些规则而进行优化. YSlow可以分析任何网站,并为每一个规则产生一个整体报告,如果页面可以进行优化,则YSlow会 ...

  4. 【题解】[CJOI2019Chebnear]

    [题解][CJOI2019Chebnear] 题目描述 给定平面上有\(n\)个仇人,\((x,y) ,x,y \in R^+\) ,\(n\)个人都是仇人关系,而有仇人关系的一对人的切比雪夫距离若\ ...

  5. Objective-c中的delegate浅析

    delegate初探 在ios开发中,我们常常会用到类似例如以下的对话框: 因此,例如以下这段代码我们也就非常熟悉了: - (IBAction)showSheet:(id)sender { UIAct ...

  6. Eclipse javax.servlet.jsp.PageContext cannot be resolved to a type 错误解决办法

    不要 直接将jsp-api.jar拷贝到lib目录下,而是通过外部jar包引用.项目 右键->Properties->Libraries->Add External JARS-选择 ...

  7. linux安装mongodb,设为全局和后台启动

    curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.5.tgz # 下载 tar -zxvf mongodb-linux ...

  8. linux下安装https证书

    https://www.aliyun.com/jiaocheng/165422.html

  9. 20145239 Linux下常用的ls命令总结

    20145239 Linux下常用的ls命令总结 通过学习本周的教学视频和要求掌握的内容,发现ls命令被使用的次数非常多,但作为一个初学者,可能我只会ls或者顶多ls -l两种用法.但其实ls是一个非 ...

  10. 图解HTTP接口自动化测试框架使用

    Robot Framework是一款python语言编写,通用的功能自动化测试框架.它使用了比较易用的表格数据语法,基于关键字驱动测试,主要用来验收测试和验收测试驱动开发(ATDD). 运行RIDE, ...