Uva LA 3177 - Beijing Guards 贪心,特例分析,判断器+二分,记录区间内状态数目来染色 难度: 3
题目
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1178
题意
圆桌上有n个人,每个人要求a_i种不同的礼物,相邻两个人的礼物不能重复,问有至少要准备多少种礼物
思路
如刘书
1. 明显,若n=1,直接输出a[0]
2. 若n为偶数,则可以形如ABABAB,直接取最大的两个相连之和
3. 若n为奇数,则可以转化为二分判断问题。
但如何判断x种礼物能否满足要求呢?n * r状态约为1e10,明显太大不能承受。
假设送第0个人,0~a[0]-1这a[0]种礼物,那么第n-1个人明显不能再拿到这a[0]种礼物了。
要找到一个贪心的原则,
由于第n-1个人编号为偶数,那么,偶数的人尽量取a[0]~x-1这些礼物,奇数的人尽量取0~a[0]-1这些礼物就可以。
但是这样如果记录每个状态时还是会出现麻烦。
那么,直接令r[i]记录第i个人拿到0~a[0]-1范围内礼物的数目,l[i]为a[0]~x-1这些礼物的数目,就能记录其状态的同时保持计算传递。
感想
1. 一开始没有注意到如果是奇数,如ABABA这种情况就会挨在一起了
2. 后来没有想到可以在延展过程中逐渐替换掉一小部分结果,而是以为要直接找个第三个最小元素的换掉,这样结果就太大了
3. 最后是没有注意到特例n=1的存在。
代码
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
const int MAXN = 1e5 + ;
int a[MAXN];
int r[MAXN], l[MAXN];
int n;
bool check(int kindNum) {
int rlimit = a[];
int llimit = kindNum - rlimit;
r[] = rlimit;
l[] = ;
for (int i = ; i < n; i++) {
if (i & ) {
r[i] = min(rlimit - r[i - ], a[i]);
l[i] = a[i] - r[i];
if (l[i] > llimit - l[i - ]) {
assert(false);
return false;
}
}
else {
l[i] = min(llimit - l[i - ], a[i]);
r[i] = a[i] - l[i];
if (r[i] > rlimit - r[i - ]) {
assert(false);
return false;
}
}
}
return r[n - ] == ;
} int main() {
#ifdef LOCAL_DEBUG
freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
//freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
for (int ti = ; scanf("%d", &n) == && n; ti++) {
int ans = ;
for (int i = ; i < n; i++) {
scanf("%d", a + i);
if (i)ans = max(ans, a[i] + a[i - ]);
}
ans = max(ans, a[n - ] + a[]);
if (n == ) ans = a[];
else if (n & ) {
int mxAns = * ans + ;
while (ans < mxAns) {
int mid = (ans + mxAns) >> ;
if (check(mid)) {
mxAns = mid;
}
else {
ans = mid + ;
}
}
}
printf("%d\n", ans); } return ;
}
Uva LA 3177 - Beijing Guards 贪心,特例分析,判断器+二分,记录区间内状态数目来染色 难度: 3的更多相关文章
- LA 3177 Beijing Guards(二分法 贪心)
Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...
- UVALive 3177 Beijing Guards
题目大意:给定一个环,每个人要得到Needi种物品,相邻的人之间不能得到相同的,问至少需要几种. 首先把n=1特判掉. 然后在n为偶数的时候,答案就是max(Needi+Needi+1)(包括(1,n ...
- UVA 1640 The Counting Problem UVA1640 求[a,b]或者[b,a]区间内0~9在里面各个数的数位上出现的总次数。
/** 题目:UVA 1640 The Counting Problem UVA1640 链接:https://vjudge.net/problem/UVA-1640 题意:求[a,b]或者[b,a] ...
- UVA-1335(UVALive-3177) Beijing Guards 贪心 二分
题面 题意:有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个相邻的人拥有同一种礼物,则双方都会很不高兴,问最少需要多少种不同的礼物才能满足所有人 ...
- UVA LA 7146 2014上海亚洲赛(贪心)
option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...
- uva 1335 - Beijing Guards(二分)
题目链接:uva 1335 - Beijing Guards 题目大意:有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个相邻的人拥有同一种礼物, ...
- 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]皇帝的烦恼 如果只是一条链,第一个护卫不与最后一个护卫相邻,那么直接贪心,找出最大的相邻数的和. 当变成环,贪心 ...
- UVA 11080 - Place the Guards(二分图判定)
UVA 11080 - Place the Guards 题目链接 题意:一些城市.之间有道路相连,如今要安放警卫,警卫能看守到当前点周围的边,一条边仅仅能有一个警卫看守,问是否有方案,假设有最少放几 ...
随机推荐
- Java用FutureTask实现又返回值的线程
要实现有返回值的多线程,具体代码如下: package thread; import java.util.concurrent.Callable; import java.util.concurren ...
- HeadFirst Ruby 第九章总结 mixins & modules
前言 如果想要复用 method, 可用的方法是针对 Class 的 inheritance,但是, inheritance has its limitations,它的缺点有: 只能 inhert ...
- 单细胞数据高级分析之初步降维和聚类 | Dimensionality reduction | Clustering
个人的一些碎碎念: 聚类,直觉就能想到kmeans聚类,另外还有一个hierarchical clustering,但是单细胞里面都用得不多,为什么?印象中只有一个scoring model是用kme ...
- spring ----> ResourceBundle [message] not found for MessageSource: Can't find bundle for base name message, local_zh
环境: idea 2018.1.3社区版,jdk8,spring4.2.0,maven3.5.2 主题: spring国际化 出现的问题: ResourceBundle [message] not f ...
- OSPF - 3,OSPF区域和LSA
1,四种末端区域骨干区域和标准区域:1,2,3,4,5,包含5类LSA,为了减少某些普通区域的LSA(主要就是4类和5类,有时做绝到连3类也不要了),引入了末梢区域.同时为了确保数据能出去,一般ABR ...
- 【异常及源码分析】org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping
一.异常出现的场景 1)异常出现的SQL @Select("SELECT\n" + " id,discount_type ,min_charge, ${cardFee} ...
- innerHTML用法及错误:无法设置未定义或null引用的属性“innerHTML”解决
在使用ActionCable时, app/assets/javascripts/channels/calladdresses.coffee: App.calladdress = App.cable.s ...
- java,数字,字符,字符串之间的转化
首先,先看一道编程题目: A除以B (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 本题要求计算A/B ...
- Vue音乐项目笔记(二)
1. Vuex https://blog.csdn.net/weixin_40814356/article/details/80347366 编写: 然后,在main.js中引入 在组件中改变stat ...
- 配置samba 服务器 共享Linux目录
配置samba 服务器 共享Linux目录 1.安装: yum install -y samba* 2.修改配置文件 vim /etc/samba/smb.conf [web] path = /usr ...