BZOJ3523 [Poi2014]Bricks 【贪心】
题目链接
题解
简单的贪心题
优先与上一个不一样且数量最多的,如果有多个相同,则优先选择非结尾颜色
比较显然,但不知怎么证
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) (cp){a,b}
#define cls(s) memset(s,0,sizeof(s))
#define LL long long int
using namespace std;
const int maxn = 1000005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int ans[maxn],n,s,t,m;
struct cp{
int cnt,id;
};
inline bool operator <(const cp& a,const cp& b){
return a.cnt == b.cnt ? a.id != t : a.cnt < b.cnt;
}
priority_queue<cp> q;
int main(){
n = read(),s = read(),t = read();
if (n == 1 && (s != t)){puts("0"); return 0;}
int tmp;
REP(i,n){
tmp = read(); m += tmp;
if (i == s) tmp--;
if (i == t) tmp--;
if (tmp == -1){puts("0"); return 0;}
if (tmp) q.push(mp(tmp,i));
}
cp u,v;
ans[1] = s; ans[m] = t;
for (int i = 2; i < m; i++){
u = q.top(); q.pop();
if (u.id == ans[i - 1]){
if (q.empty()){puts("0"); return 0;}
v = q.top(); q.pop();
ans[i] = v.id; v.cnt--;
q.push(u);
if (v.cnt) q.push(v);
}
else {
ans[i] = u.id; u.cnt--;
if (u.cnt) q.push(u);
}
}
if (ans[m - 1] == t){puts("0"); return 0;}
for (int i = 1; i <= m; i++)
printf("%d ",ans[i]);
return 0;
}
BZOJ3523 [Poi2014]Bricks 【贪心】的更多相关文章
- BZOJ3523[Poi2014]Bricks——贪心+堆
题目描述 有n种颜色的砖块,第i种颜色的砖块有a[i]个,你需要把他们放成一排,使得相邻两个砖块的颜色不相同,限定第一个砖块的颜色是start,最后一个砖块的颜色是end,请构造出一种合法的方案或判断 ...
- 【BZOJ】3523: [Poi2014]Bricks
题意 \(n(n \le 1000000)\)个物品,颜色分别为\(a[i]\),现在要求排在一排使得相邻两个砖块的颜色不同,且限定第一个砖块和最后一个砖块的颜色,输出一个合法解否则输出-1. 分析 ...
- [BZOJ3523][Poi2014]KLO-Bricks——全网唯一 一篇O(n)题解+bzoj最优解
Description 有n种颜色的砖块,第i种颜色的砖块有a[i]个,你需要把他们放成一排,使得相邻两个砖块的颜色不相同,限定第一个砖块的颜色是start,最后一个砖块的颜色是end,请构造出一种合 ...
- [BZOJ3829][Poi2014]FarmCraft 贪心
这个题应该是很容易想到贪心的,只要可是怎么贪才是科学的呢?我们分析一下题干,对于每个边只能一进一出因此,对于树上的一棵子树,我们只要一进子树就必须遍历完,因此我们只能进行一遍 dfs() 然后我们发现 ...
- 【bzoj3829】[Poi2014]FarmCraft 贪心
原文地址:http://www.cnblogs.com/GXZlegend/p/6826667.html 题目描述 In a village called Byteville, there are ...
- POI2014题解
POI2014题解 [BZOJ3521][Poi2014]Salad Bar 把p当作\(1\),把j当作\(-1\),然后做一遍前缀和. 一个合法区间\([l,r]\)要满足条件就需要满足所有前缀和 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- Codeforces Round #551 (Div. 2) A-E
A. Serval and Bus 算出每辆车会在什么时候上车, 取min即可 #include<cstdio> #include<algorithm> #include< ...
- [POI2014]FAR-FarmCraft 树形DP + 贪心思想
(感觉洛谷上题面那一小段中文根本看不懂啊,好多条件都没讲,直接就是安装也要一个时间啊,,,明明不止啊!还好有百度翻译......) 题意:一棵树,一开始在1号节点(root),边权都为1,每个点有点权 ...
随机推荐
- Linux命令应用大词典-第2章 获取帮助
2.1 help:查看内部Shell命令帮助信息 2.2 man:显示在线手册页 2.3 manpath:查看和设置man手册页的查询路径 2.4 info:阅读info格式的文件 2.5 pinfo ...
- python编程os、os.path 模块中关于文件、目录常用的函数使用方法
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名( ...
- 【checkbox-group、checkbox】 多项选择器组件说明
checkbox-group组件包裹checkbox组件的容器 原型: <check-group bindchange="[EventHandle]"> <che ...
- Linux系统负载查询
查询Linux系统负载情况,一般需要了解三个方面的信息: 1.Linux系统配置.如Linux版本号.CPU.内存.网络.磁盘等: 2.收集系统负载信息的手段.常用的工具包有sysstat和procp ...
- LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- Bus of Characters(栈和队列)
In the Bus of Characters there are nn rows of seat, each having 22 seats. The width of both seats in ...
- “Hello world!”团队第一周贡献分分配结果
小组名称:Hello World! 项目名称:空天猎 组长:陈建宇 成员:刘成志.阚博文.刘淑霞.黄泽宇.方铭.贾男男 第一周贡献分分配结果 基础分 会议分 提功能分 个人表现分 各项总分 最终分 ...
- Notes of the scrum meeting(12.9)
meeting time:14:00~17:00p.m.,December 9th,2013 meeting place:一号教学楼209 attendees: 顾育豪 ...
- 对Objective-C中runtime的理解
Objective-C是面向runtime(运行时)的语言,在应用程序运行的时候来决定函数内部实现什么以及做出其它决定的语言.程序员可以在程序运行时创建,检 查,修改类,对象和它们的方法,Object ...
- PokeCats开发者日志(十四)——终章
已经不知道离PokeCats游戏开始开发有多少个晚上了,今晚心血来潮整理随笔的时候发现这个故事还没有划上句号. 故事的结局是最终我拿到了软著权,但是没办法上架到任何一个知名的安卓app市场,连 ...