Painting The Fence(贪心+优先队列)

题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1".

解题思路:贪心的思路,优先选择数量多的先填,这样会让最后剩余相同的数字数量最少,所以我们优先选数量最多的两种数字填,最后剩下的(某一种)就填到它前面的位置去,一定是和相同的填在一起,这里就不证明了,自己画下就可以得到。优先队列模拟即可。

AC_Code

 /* */
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <utility>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,1,sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9+;
const int inf=0x3f3f3f3f;
const int INF=0x3f3f3f3f3f3f3f3f;
const double eps=0.000001;
const double pi=acos(-1.0); int n,m,k;
int a[],s[]; struct node///重载,从大到小排序
{
int bianhao,num;
bool operator<(const node &a) const {
return num<a.num;
}
}; priority_queue<node>q; int main()
{
while( ~ scanf("%d%d%d",&n,&m,&k))
{
mem0(a);
mem0(s);
while(!q.empty()) q.pop();
for(int i=; i<=m;i++)
{
node cur;
scanf("%d",&cur.num);
cur.bianhao=i;
q.push(cur);
}
int cnt=;
while( !q.empty() )
{
if( q.size()>= )
{
node n1=q.top(); q.pop();
node n2=q.top(); q.pop();
a[cnt++]=n1.bianhao;
a[cnt++]=n2.bianhao;
n1.num--;
n2.num--;
if( n1.num ) q.push(n1);
if( n2.num ) q.push(n2);
}
else
{
if( a[cnt-]!=q.top().bianhao )
{
node cur=q.top();q.pop();
a[cnt++]=cur.bianhao;
cur.num--;
if(cur.num) q.push(cur);
}
break;
}
}
for(int i=; i<cnt&&!q.empty(); i++)
{
if( a[i]==q.top().bianhao )
{
s[i]+=min(q.top().num,k-);
node cur=q.top();q.pop();
cur.num-=min(cur.num,k-);
if( cur.num ) q.push(cur);
else break;
}
}
if( !q.empty()) printf("-1\n");
else
{
for(int i=;i<cnt;i++)
{
for(int j=;j<=s[i]; j++)
{
printf("%d ",a[i]);
}
}
printf("\n");
}
}
return ;
}

Painting The Fence(贪心+优先队列)的更多相关文章

  1. hihoCoder 1309:任务分配 贪心 优先队列

    #1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN,  ...

  2. UVA 11134 - Fabled Rooks(贪心+优先队列)

    We would like to place  n  rooks, 1 ≤  n  ≤ 5000, on a  n×n  board subject to the following restrict ...

  3. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  4. C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列

    C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  5. [luogu P2205] [USACO13JAN]画栅栏Painting the Fence

    [luogu P2205] [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to p ...

  6. 洛谷 画栅栏Painting the Fence 解题报告

    P2205 画栅栏Painting the Fence 题目描述 \(Farmer\) \(John\) 想出了一个给牛棚旁的长围墙涂色的好方法.(为了简单起见,我们把围墙看做一维的数轴,每一个单位长 ...

  7. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  8. 贪心+优先队列 HDOJ 5360 Hiking

    题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...

  9. [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)

    传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...

随机推荐

  1. 2019 世纪龙java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.世纪龙等公司offer,岗位是Java后端开发,因为发展原因最终选择去了世纪龙,入职一年时间了,也成为了面试官 ...

  2. 一个牛逼的 Python 调试工具PySnooper

    原文转自:https://mp.weixin.qq.com/s/OtLr-cNethboMgmCcUx2pA PySnooper 使用起来十分简单,开发者可以在任何庞大的代码库中使用它,而无需进行任何 ...

  3. Ubuntu中使用sanp一键安装安装Notepad ++

    很少有文本编辑器像Notepad ++一样流行得到广大用户的喜爱,Notepad ++是一个免费的开源代码编辑器,专为Windows构建,用C ++编写.以其小巧的应用程序大小和出色的性能而闻名,但缺 ...

  4. iOS - 架构模式 - 解密 MVC、MVP、MVVM、VIPER架构

    在 iOS 中使用 MVC 架构感觉很奇怪? 迁移到MVVM架构又怀有疑虑?听说过 VIPER 又不确定是否真的值得切换? 相信你会找到以上问题的答案,如果没找到请在评论中指出. 你将要整理出你在 i ...

  5. 对于flex布局的使用心得

    弹性盒子flex: 对于客户端的布局非常有用,不管是平均分配space-around这个属性还是两端对齐space-betwee在页面布局的时候都会有很好的表现. 对于部分内容区域中,具有很多大致内容 ...

  6. 大数据之kafka-02.搞定kafka专业术语

    02.搞定kafka专业术语 在kafka的世界中有很多概念和术语是需要我们提前理解并且熟练掌握的,下面来盘点一下. 之前我们提到过,kafka属于分布式的消息引擎系统,主要功能是提供一套完善的消息发 ...

  7. django模板和静态文件

    1.为什么要使用模板 在上一篇博文中,提到了HttpReponse,但是HttpReponse只能传送字符串,如果要构建一个网页,那么工作量就会十分巨大.模板是一种方便的标签,存在于HTML文件中,我 ...

  8. 剖析和解决Python中网络粘包的正确姿势

    目录 1.粘包及其成因 1.1.粘包产生 1.2.粘包产生的原因 2.尝试解决粘包 2.1.指定数据包的长度 2.2.固定数据包的长度 2.3.用函数实现多次调用发送数据 3.解决粘包问题的正确姿势 ...

  9. spring的声明式事务和编程式事务

    事务管理对于企业应用来说是至关重要的,当出现异常情况时,它可以保证数据的一致性. Spring事务管理的两种方式 1.编程式事务 使用Transaction Ttempleate或者直接使用底层的Pl ...

  10. Centos7-Gnome安装

    查看grouplist yum grouplist 安装gnome yum groupinstall "GNOME Desktop" root用户权限下,设置centos系统默认的 ...