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,每个点有点权 ...
 
随机推荐
- thinkphp5
			
分页: thinkphp5分页默认只带page参数 在使用form表单method='get'传递关键字来筛选: 保证每次刷新依旧带上筛选参数 但遇到分页时,下面的分页默认自带page,没有之前筛选的 ...
 - hdu1052Tian Ji -- The Horse Racing(贪心,细节多)
			
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
 - VIN码识别:毫秒间99%精准识别
			
科技改变生活.几年前,人工智能还仅是一个噱头,现在已逐渐融入我们的工作和生活,代替了一些重复性的.繁杂的人工劳动,在提高工作效率的同时,提升了客户体验. 车架号也叫VIN码,由17位数字和字母混合组成 ...
 - 【SpringCloud】第五篇: 路由网关(zuul)
			
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
 - Java开发工程师(Web方向) - 04.Spring框架 - 第1章.Spring概述
			
第1章.Spring概述 Spring概述 The Spring Framework is a lightweight solution and a potential one-stop-shop f ...
 - [JSON].getObj( keyPath )
			
语法:[JSON].getObj( keyPath ) 返回:[JSON] 说明:返回指定键名路径的JSON对象,指定键名路径不存在时返回空的toJson对象(强烈建议使用 [JSON].exists ...
 - 158. Valid Anagram【LintCode by java】
			
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
 - 珍珠 Median Weight Bead 977
			
描述 There are N beads which of the same shape and size, but with different weights. N is an odd numbe ...
 - [2017 - 2018 ACL] 对话系统论文研究点整理
			
(论文编号及摘要见 [2017 ACL] 对话系统. [2018 ACL Long] 对话系统. 论文标题[]中最后的数字表示截止2019.1.21 google被引次数) 1. Domain Ada ...
 - mweb test
			
目录 Markdown syntax guide and writing on MWeb Philosophy Notice Headers This is an <h1> tag Thi ...