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

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

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

AC_Code

  1. /* */
  2. #include <bits/stdc++.h>
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <ctime>
  6. #include <cmath>
  7. #include <utility>
  8. #include <algorithm>
  9. #include <cstring>
  10. #include <queue>
  11. #include <vector>
  12. #include <queue>
  13. #include <map>
  14. #include <set>
  15. #include <stack>
  16. using namespace std;
  17. #define mem0(a) memset(a,0,sizeof(a))
  18. #define mem1(a) memset(a,1,sizeof(a))
  19. typedef long long ll;
  20. typedef unsigned long long ull;
  21. const ll mod = 1e9+;
  22. const int inf=0x3f3f3f3f;
  23. const int INF=0x3f3f3f3f3f3f3f3f;
  24. const double eps=0.000001;
  25. const double pi=acos(-1.0);
  26.  
  27. int n,m,k;
  28. int a[],s[];
  29.  
  30. struct node///重载,从大到小排序
  31. {
  32. int bianhao,num;
  33. bool operator<(const node &a) const {
  34. return num<a.num;
  35. }
  36. };
  37.  
  38. priority_queue<node>q;
  39.  
  40. int main()
  41. {
  42. while( ~ scanf("%d%d%d",&n,&m,&k))
  43. {
  44. mem0(a);
  45. mem0(s);
  46. while(!q.empty()) q.pop();
  47. for(int i=; i<=m;i++)
  48. {
  49. node cur;
  50. scanf("%d",&cur.num);
  51. cur.bianhao=i;
  52. q.push(cur);
  53. }
  54. int cnt=;
  55. while( !q.empty() )
  56. {
  57. if( q.size()>= )
  58. {
  59. node n1=q.top(); q.pop();
  60. node n2=q.top(); q.pop();
  61. a[cnt++]=n1.bianhao;
  62. a[cnt++]=n2.bianhao;
  63. n1.num--;
  64. n2.num--;
  65. if( n1.num ) q.push(n1);
  66. if( n2.num ) q.push(n2);
  67. }
  68. else
  69. {
  70. if( a[cnt-]!=q.top().bianhao )
  71. {
  72. node cur=q.top();q.pop();
  73. a[cnt++]=cur.bianhao;
  74. cur.num--;
  75. if(cur.num) q.push(cur);
  76. }
  77. break;
  78. }
  79. }
  80. for(int i=; i<cnt&&!q.empty(); i++)
  81. {
  82. if( a[i]==q.top().bianhao )
  83. {
  84. s[i]+=min(q.top().num,k-);
  85. node cur=q.top();q.pop();
  86. cur.num-=min(cur.num,k-);
  87. if( cur.num ) q.push(cur);
  88. else break;
  89. }
  90. }
  91. if( !q.empty()) printf("-1\n");
  92. else
  93. {
  94. for(int i=;i<cnt;i++)
  95. {
  96. for(int j=;j<=s[i]; j++)
  97. {
  98. printf("%d ",a[i]);
  99. }
  100. }
  101. printf("\n");
  102. }
  103. }
  104. return ;
  105. }

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. C# vb .net实现位图蒙版特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的位图蒙版特效呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...

  2. 局域网电脑禁止ping通的解决方法

    方法1:命令行模式进入服务器后 点击 开始——运行 输入命令:netsh firewall set icmpsetting 8这样就可以在外部ping到服务器了 非常简单实用!同样道理,如果想禁止Pi ...

  3. Linux的巡检命令

    # uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue # 查看操作系统版本# cat /proc/cpuinfo # 查看CPU信息# hostname ...

  4. Java基础之枚举类型Enum的使用

    Java基础之枚举类型Enum的使用 定义 public enum AccruedCleanEnum { SPREAD("1","发票"),OTHER(&quo ...

  5. 协程和Goroutines示例

    一. 协程的定义 Coroutines are computer-program components that generalize subroutines for non-preemptive m ...

  6. Oracle大表改为分区表及表空间切换方案

    Oracle大表改为分区表及表空间切换方案 一.            背景 由于之前数据库表和索引放在一个表空间导致表空间数据文件增长太快,文件数量即将达到Oracle表空间的限制,需要对表(没有分 ...

  7. thrift java first demo

    参考文档:http://thrift.apache.org/ 1.下载需要的文件 地址:http://thrift.apache.org/download  需要下载 thrift-0.12.0.ta ...

  8. 木马防杀 花指令 OllyDbg

    打开木马 入口地址 添加花指令 全0的地方,可以插入花指令 保存为可执行文件 随便选择几行,右击 保存文件

  9. SVN 报错 Can't install '*' from pristine store, because no checksum is recorded for this file

    SVN同步.cleanup都会出现下面的提示: svn: E155017: Can't install '*' from pristine store, because no checksum is ...

  10. JavaWeb项目目录结构

    今天本来是准备学习spring mvc + mybatis,结果被idea的配置环境卡主了,自己刚刚接触JavaWeb不久,所以浪费了很多时间.最终我回归最简单的servlet & jsp,并 ...