(贪心+队列)String
http://acm.hdu.edu.cn/showproblem.php?pid=6586
将26个字母各自放入队列中,并记下每个位置各字母的后缀和。往k个位置贪心的放字母,先从a开始尝试。首先字母的数量不能大于最大限制,然后位置要大于上一个放的字母。要放这个字母还要满足两个条件。
1.对于26个字母,其后的个数加上当前选的个数要小于l[i]。
2.对于26个字母,把其后的个数取最小之后的和不能大于k,把其后的个数取最大之后的和不能小于k。
- #include<cstring>
- #include<algorithm>
- #include<vector>
- #include<map>
- #include<queue>
- #include<cstdio>
- #include<stack>
- #include<cmath>
- #include<iostream>
- #include<set>
- #define ll long long
- #define lowbit(x) x&(-x)
- using namespace std;
- const int MAX = ;
- int l[MAX],r[MAX],num[MAX];
- int sum[][MAX];
- char s[],ans[];
- int len,k;
- void init()
- {
- for (int i = ; i < ;i++)
- {
- sum[len+][i]=;
- num[i]=;
- }
- }
- int main()
- {
- while(~scanf("%s%d",s+,&k))
- {
- queue<int> q[];
- len=strlen(s+);
- init();
- for (int i = ; i < ;i++)
- scanf("%d %d",&l[i],&r[i]);
- for (int i = ; i <= len;i++)
- q[s[i]-'a'].push(i);
- for (int i = len; i >= ;i--)
- {
- for (int j = ; j < ;j++)
- {
- sum[i][j]=sum[i+][j]+(s[i]==('a'+j));
- }
- }
- int now=;
- bool flag=false;
- int cntt=;
- for (int i = ; i <= k;i++)
- {
- for (int j = ; j < ;j++)
- {
- if(num[j]==r[j])
- continue;
- while(!q[j].empty()&&now>=q[j].front()) q[j].pop();
- if(!q[j].empty())
- {
- bool f=false;
- int cnt=q[j].front();
- num[j]++;
- for (int ii = ; ii < ;ii++)
- {
- if(num[ii]+sum[cnt+][ii]<l[ii]){
- num[j]--;
- f=true;
- break;
- }
- }
- if(f) continue;
- int minn,maxx;
- minn=maxx=;
- for (int ii = ; ii < ;ii++)
- {
- minn+=max(l[ii]-num[ii],);
- maxx+=min(r[ii]-num[ii],sum[cnt+][ii]);
- }
- if(minn+i<=k&&maxx+i>=k)
- {
- ans[cntt++]=('a'+j);
- now=q[j].front();
- break;
- }
- else
- num[j]--;
- }
- }
- if(cntt!=i)
- break;
- }
- if(cntt==k)
- {
- ans[k]='\0';
- printf("%s\n",ans);
- }
- else
- printf("-1\n");
- }
- return ;
- }
(贪心+队列)String的更多相关文章
- [Swust OJ 352]--合并果子(贪心+队列模拟)
题目链接:http://acm.swust.edu.cn/problem/352/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- 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 ...
- 51nod 1672 贪心/队列
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672 1672 区间交 基准时间限制:1 秒 空间限制:131072 K ...
- Codeforces 1500D - Tiles for Bathroom(贪心+队列)
Codeforces 题面传送门 & 洛谷题面传送门 首先先讲一发我的 \(n^2q\log n\) 的做法,虽然没有付诸实现并且我也深知它常数巨大过不去,但是我还是决定讲一讲(大雾 考虑设 ...
- 消息队列——RabbitMQ学习笔记
消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...
- 总结消息队列RabbitMQ的基本用法
一.RabbitMQ是什么? AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.消息中间件主要用于组 ...
- 分布式日志2 用redis的队列写日志
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- RabbitMQ队列
AMQP ,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.消息中间件主要用于组件之间的解耦,消息的发送者无 ...
- [转载] 基于Redis实现分布式消息队列
转载自http://www.linuxidc.com/Linux/2015-05/117661.htm 1.为什么需要消息队列?当系统中出现“生产“和“消费“的速度或稳定性等因素不一致的时候,就需要消 ...
随机推荐
- 使用powershell管理Docker Toolbox
打开PowerShell,输入: docker-machine ls 我们可以看到我们当前的Docker虚拟机的状态.如果什么都没有的话,那么我们可以使用以下命令创建一个Docker虚拟机. dock ...
- 1000行基本SQL
/* Windows服务 */ -- 启动MySQL net start mysql -- 创建Windows服务 sc create mysql binPath= mysqld_bin_path(注 ...
- PhoneGap学习网址
官网:http://app-framework-software.intel.com/ 下载地址:http://download.csdn.net/download/haozq2012/7635951
- 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 ...
- P1079Vigenère密码
这是2012年noip提高组的的DAY1T1,我用了一下午的时间,一次性AC^^. 这是一个字符串的模拟题.首先给出了一个密码对应法则,我们发现在同一对角线的明文通过密钥得出来的密文是相同的.根据八皇 ...
- 使用autotools自动生成Makefile并在此之上使用dh-make生成可发布的deb程序包(详解)
转自:http://blog.csdn.net/longerzone/article/details/12705507 一.前言 本文将介绍如何使用autotools生成一个Makefile文件,并在 ...
- 有序无序ul->li ol->li菜单,默认点击当前弹出下拉,再次点击收起下拉菜单
实现这一效果利用css和js技术结合 以ul->li为例子 <!DOCTYPE html><html lang="en"><head> & ...
- 01.AutoMapper 之约定(Conventions)
转载(https://www.jianshu.com/p/d4c472d95da4) 约定(Conventions) 条件对象映射器 条件对象映射器根据源类型和目标类型之间的条件生成新类型映射. ...
- iOS之Run Loop详解
转自标哥的技术博客(www.henishuo.com) 前言 做了一年多的IOS开发,对IOS和Objective-C深层次的了解还十分有限,大多还停留在会用API的级别,这是件挺可悲的事情.想学好一 ...
- lilo.conf - lilo 配置文件
描述 默认情况下,本文件 ( /etc/lilo.conf ) 由引导管理程序 lilo 读取 (参考 lilo(8)). 它看起来可能象这样: boot = /dev/hda delay = 40 ...