UVa 1335 Beijing Guards (二分+贪心)
题意: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 (二分+贪心)的更多相关文章
- uva 1335 - Beijing Guards(二分)
题目链接:uva 1335 - Beijing Guards 题目大意:有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个相邻的人拥有同一种礼物, ...
- 【二分答案+贪心】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(二分答案)
入口: https://cn.vjudge.net/problem/UVA-1335 [题意] 有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个 ...
- uva 1335 - Beijing Guards
竟然用二分,真是想不到: 偶数的情况很容易想到:不过奇数的就难了: 奇数的情况下,一个从后向前拿,一个从前向后拿的分配方法实在太妙了! 注: 白书上的代码有一点点错误 代码: #include< ...
- Uva 长城守卫——1335 - Beijing Guards
二分查找+一定的技巧 #include<iostream> using namespace std; +; int n,r[maxn],Left[maxn],Right[maxn];//因 ...
- UVA 1149 Bin Packing 二分+贪心
A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...
- LA 3177 Beijing Guards(二分法 贪心)
Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...
- LA3177 Beijing Guards
Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...
- 题解 UVA1335 【Beijing Guards】
UVA1335 Beijing Guards 双倍经验:P4409 [ZJOI2006]皇帝的烦恼 如果只是一条链,第一个护卫不与最后一个护卫相邻,那么直接贪心,找出最大的相邻数的和. 当变成环,贪心 ...
随机推荐
- EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:内部搜索功能的实现
上一篇介绍了处理接口获取的数据,本篇将介绍如何在接收到的数据中搜索出自己符合条件的数据: 为了页面的美观,我们往往会以分页的形式来进行数据的展示.但是,当需要展示出来的数据太多的时候,我们很难迅速的找 ...
- 九度OJ 1016:火星A+B (进制转换)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4913 解决:1334 题目描述: 读入两个不超过25位的火星正整数A和B,计算A+B.需要注意的是:在火星上,整数不是单一进制的, ...
- iOS改变UIAlertView、UIActionSheet、UIAlertController系统字体颜色
废话不多说,直接上代码,效果是最好的说服力 1.改变UIAlertView字体颜色 [UIView appearance].tintColor = [UIColor greenColor]; 个人还是 ...
- SASL mechanism
<property> <name>hive.spark.client.rpc.sasl.mechanisms</name> <value>DIGEST- ...
- Python爬虫-- BeautifulSoup库
BeautifulSoup库 beautifulsoup就是一个非常强大的工具,爬虫利器.一个灵活又方便的网页解析库,处理高效,支持多种解析器.利用它就不用编写正则表达式也能方便的实现网页信息的抓取 ...
- linux c编程:进程间通信
进程间的通信包括管道,共享内存,信号量通信,消息队列,套借口(socket)和全双工管道通信 首先来看下管道的用法:管道顾名思义,就如同下水道管道一样,当从管道一端流水到另一端的时候,水流的方向是单方 ...
- Chart.js 动态图表的使用
一.相关资料 1. 简介 Chart.js 是一个基于 HTML5 的简单的面向对象的图表库,支持包括 IE7/8 和所有现代浏览器.支持六种图标:曲线图(Linecharts).柱状图(Barcha ...
- LeetCode:将有序数组转换为二叉搜索树【108】
LeetCode:将有序数组转换为二叉搜索树[108] 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差 ...
- 创建一个HTML5与JQuery结合的幻灯片
1. [代码][JavaScript]代码 $(window).load(function(){ //我们监听了 window.load 事件,因此我们确定幻灯片上的所有图片都能够正确进行加载. ...
- ios图文混编瀑布流
ios图文混编瀑布流,利用UICollectionView 实现图文混编的瀑布流,支持section内容伸缩 http://www.huiyi8.com/pubuliu/