http://acm.hdu.edu.cn/showproblem.php?pid=6586

将26个字母各自放入队列中,并记下每个位置各字母的后缀和。往k个位置贪心的放字母,先从a开始尝试。首先字母的数量不能大于最大限制,然后位置要大于上一个放的字母。要放这个字母还要满足两个条件。

1.对于26个字母,其后的个数加上当前选的个数要小于l[i]。

2.对于26个字母,把其后的个数取最小之后的和不能大于k,把其后的个数取最大之后的和不能小于k。

  1. #include<cstring>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<map>
  5. #include<queue>
  6. #include<cstdio>
  7. #include<stack>
  8. #include<cmath>
  9. #include<iostream>
  10. #include<set>
  11. #define ll long long
  12. #define lowbit(x) x&(-x)
  13. using namespace std;
  14. const int MAX = ;
  15. int l[MAX],r[MAX],num[MAX];
  16. int sum[][MAX];
  17. char s[],ans[];
  18. int len,k;
  19. void init()
  20. {
  21. for (int i = ; i < ;i++)
  22. {
  23. sum[len+][i]=;
  24. num[i]=;
  25. }
  26. }
  27. int main()
  28. {
  29. while(~scanf("%s%d",s+,&k))
  30. {
  31. queue<int> q[];
  32. len=strlen(s+);
  33. init();
  34. for (int i = ; i < ;i++)
  35. scanf("%d %d",&l[i],&r[i]);
  36. for (int i = ; i <= len;i++)
  37. q[s[i]-'a'].push(i);
  38. for (int i = len; i >= ;i--)
  39. {
  40. for (int j = ; j < ;j++)
  41. {
  42. sum[i][j]=sum[i+][j]+(s[i]==('a'+j));
  43. }
  44. }
  45. int now=;
  46. bool flag=false;
  47. int cntt=;
  48. for (int i = ; i <= k;i++)
  49. {
  50. for (int j = ; j < ;j++)
  51. {
  52. if(num[j]==r[j])
  53. continue;
  54. while(!q[j].empty()&&now>=q[j].front()) q[j].pop();
  55. if(!q[j].empty())
  56. {
  57. bool f=false;
  58. int cnt=q[j].front();
  59. num[j]++;
  60. for (int ii = ; ii < ;ii++)
  61. {
  62. if(num[ii]+sum[cnt+][ii]<l[ii]){
  63. num[j]--;
  64. f=true;
  65. break;
  66. }
  67. }
  68. if(f) continue;
  69. int minn,maxx;
  70. minn=maxx=;
  71. for (int ii = ; ii < ;ii++)
  72. {
  73. minn+=max(l[ii]-num[ii],);
  74. maxx+=min(r[ii]-num[ii],sum[cnt+][ii]);
  75. }
  76. if(minn+i<=k&&maxx+i>=k)
  77. {
  78. ans[cntt++]=('a'+j);
  79. now=q[j].front();
  80. break;
  81. }
  82. else
  83. num[j]--;
  84. }
  85. }
  86. if(cntt!=i)
  87. break;
  88. }
  89. if(cntt==k)
  90. {
  91. ans[k]='\0';
  92. printf("%s\n",ans);
  93. }
  94. else
  95. printf("-1\n");
  96. }
  97. return ;
  98. }

(贪心+队列)String的更多相关文章

  1. [Swust OJ 352]--合并果子(贪心+队列模拟)

    题目链接:http://acm.swust.edu.cn/problem/352/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  2. TTTTTTTTTTTTTT CF 95 B 构造4,7幸运数字 贪心 构造 string

    B. Lucky Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. 51nod 1672 贪心/队列

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672 1672 区间交 基准时间限制:1 秒 空间限制:131072 K ...

  4. Codeforces 1500D - Tiles for Bathroom(贪心+队列)

    Codeforces 题面传送门 & 洛谷题面传送门 首先先讲一发我的 \(n^2q\log n\) 的做法,虽然没有付诸实现并且我也深知它常数巨大过不去,但是我还是决定讲一讲(大雾 考虑设 ...

  5. 消息队列——RabbitMQ学习笔记

    消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...

  6. 总结消息队列RabbitMQ的基本用法

    一.RabbitMQ是什么? AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.消息中间件主要用于组 ...

  7. 分布式日志2 用redis的队列写日志

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  8. RabbitMQ队列

    AMQP ,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.消息中间件主要用于组件之间的解耦,消息的发送者无 ...

  9. [转载] 基于Redis实现分布式消息队列

    转载自http://www.linuxidc.com/Linux/2015-05/117661.htm 1.为什么需要消息队列?当系统中出现“生产“和“消费“的速度或稳定性等因素不一致的时候,就需要消 ...

随机推荐

  1. 使用powershell管理Docker Toolbox

    打开PowerShell,输入: docker-machine ls 我们可以看到我们当前的Docker虚拟机的状态.如果什么都没有的话,那么我们可以使用以下命令创建一个Docker虚拟机. dock ...

  2. 1000行基本SQL

    /* Windows服务 */ -- 启动MySQL net start mysql -- 创建Windows服务 sc create mysql binPath= mysqld_bin_path(注 ...

  3. PhoneGap学习网址

    官网:http://app-framework-software.intel.com/ 下载地址:http://download.csdn.net/download/haozq2012/7635951

  4. The order of a Tree

    The order of a Tree Problem Description As we know,the shape of a binary search tree is greatly rela ...

  5. P1079Vigenère密码

    这是2012年noip提高组的的DAY1T1,我用了一下午的时间,一次性AC^^. 这是一个字符串的模拟题.首先给出了一个密码对应法则,我们发现在同一对角线的明文通过密钥得出来的密文是相同的.根据八皇 ...

  6. 使用autotools自动生成Makefile并在此之上使用dh-make生成可发布的deb程序包(详解)

    转自:http://blog.csdn.net/longerzone/article/details/12705507 一.前言 本文将介绍如何使用autotools生成一个Makefile文件,并在 ...

  7. 有序无序ul->li ol->li菜单,默认点击当前弹出下拉,再次点击收起下拉菜单

    实现这一效果利用css和js技术结合 以ul->li为例子 <!DOCTYPE html><html lang="en"><head> & ...

  8. 01.AutoMapper 之约定(Conventions)

    转载(https://www.jianshu.com/p/d4c472d95da4)   约定(Conventions) 条件对象映射器 条件对象映射器根据源类型和目标类型之间的条件生成新类型映射. ...

  9. iOS之Run Loop详解

    转自标哥的技术博客(www.henishuo.com) 前言 做了一年多的IOS开发,对IOS和Objective-C深层次的了解还十分有限,大多还停留在会用API的级别,这是件挺可悲的事情.想学好一 ...

  10. lilo.conf - lilo 配置文件

    描述 默认情况下,本文件 ( /etc/lilo.conf ) 由引导管理程序 lilo 读取 (参考 lilo(8)). 它看起来可能象这样: boot = /dev/hda delay = 40 ...