CF 335A(Banana-贪心-priority_queue是大根堆)
2 seconds
256 megabytes
standard input
standard output
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n(1 ≤ n ≤ 1000).
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
banana
4
2
baan
banana
3
3
nab
banana
2
-1
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
最近的比赛时间都是什么心态。。。
这题贪心很直观了吧。
显然字母可以分开来考虑。。每次选取该字母需要sticker最多的,+上该字母
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<queue>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000+10)
#define MP(a,b) make_pair(a,b)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
typedef pair<int,char> pic;
int n,m;
char s[MAXN],ans[MAXN];
int c[MAXN]={0};
struct node
{
int a,b;
char c;
node(){}
node(int _a,char _c):a(_a),b(1),c(_c){}
int v(){return a/b+(bool)(a%b);}
friend bool operator<(node a,node b){return a.v()<b.v(); }
};
priority_queue< node > h;
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
scanf("%s%d",s+1,&m);ans[m+1]=0;n=strlen(s+1);
For(i,n) c[s[i]]++;
for(int i='a';i<='z';i++) if (c[i]) h.push(node(c[i],i)),ans[h.size()]=i;
if (h.size()>m) cout<<"-1"<<endl;
else
{
Fork(i,h.size()+1,m)
{
node p=h.top();
h.pop();
ans[i]=p.c;
p.b++;
h.push(p);
}
node p=h.top();
cout<<p.v()<<endl;
printf("%s\n",ans+1);
} return 0;
}
CF 335A(Banana-贪心-priority_queue是大根堆)的更多相关文章
- codeforces 335A Banana(贪心)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Banana Piegirl is buying stickers for ...
- bzoj 1577: [Usaco2009 Feb]庙会捷运Fair Shuttle——小根堆+大根堆+贪心
Description 公交车一共经过N(1<=N<=20000)个站点,从站点1一直驶到站点N.K(1<=K<=50000)群奶牛希望搭乘这辆公交车.第i群牛一共有Mi(1& ...
- priority_queue()大根堆和小根堆(二叉堆)
#include<iostream> #include <queue> using namespace std; int main() { //对于基础类型 默认是大顶堆 pr ...
- AtCoder Beginner Contest 249 F - Ignore Operations // 贪心 + 大根堆
传送门:F - Keep Connect (atcoder.jp) 题意: 给定长度为N的操作(ti,yi). 给定初值为0的x,对其进行操作:当t为1时,将x替换为y:当t为2时,将x加上y. 最多 ...
- 让priority_queue支持小根堆的几种方法
点击这里了解什么是priority_queue 前言 priority_queue默认是大根堆,也就是大的元素会放在前面 例如 #include<iostream> #include< ...
- CJOJ 2482 【POI2000】促销活动(STL优先队列,大根堆,小根堆)
CJOJ 2482 [POI2000]促销活动(STL优先队列,大根堆,小根堆) Description 促销活动遵守以下规则: 一个消费者 -- 想参加促销活动的消费者,在账单下记下他自己所付的费用 ...
- 51nod K 汽油补给 大根堆+小根堆....
题目传送门 用优先队列瞎搞... 想着在每个地方 先算上一个点到这一个点要花费多少钱 这个用小根堆算就好 然后在这个地方加油 把油钱比自己多的替代掉 这个用大根堆维护一下 然后两个堆之间信息要保持互通 ...
- bzoj 5495: [2019省队联测]异或粽子【可持久化trie+大根堆】
和bzoj4504差不多,就是换了个数据结构 像超级钢琴一样把五元组放进大根堆,每次取一个出来拆开,(d,l,r,p,v)表示右端点为d,左端点区间为(l,r),最大区间和值为v左端点在p上 关于怎么 ...
- bzoj 4504: K个串【大根堆+主席树】
像超级钢琴一样把五元组放进大根堆,每次取一个出来拆开,(d,l,r,p,v)表示右端点为d,左端点区间为(l,r),最大区间和值为v左端点在p上 关于怎么快速求区间和,用可持久化线段树维护(主席树?) ...
随机推荐
- java多线程之Lock线程同步
1.线程同步: package cn.itcast.heima2; import java.util.concurrent.locks.Lock; import java.util.concurren ...
- 获取子窗口中使用jQuery.data()设置的参数
http://hyj1254.iteye.com/blog/643035 假设在iframe子窗口中设置了$('#mydata').data('key','hello world'); 那在包含ifr ...
- HW7.13
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- Systemd Unit文件中PrivateTmp字段详解-Jason.Zhi
如下图,在开发调试的时候会遇到这么一个问题. file_put_contents时,$tmp_file显示的目标文件是/tmp/xxx.而这个文件实际放在linux的目录却是/tmp/systemd- ...
- rabbitMQ 远程访问
AMQP server localhost:5672 closed the connection. Check login credentials: Socket closed root@ruiy-c ...
- 指令式Callback,函数式Promise:对node.js的一声叹息
原文:Callbacks are imperative, promises are functional: Node's biggest missed opportunity promises 天生就 ...
- commondline 之三 执行jar文件
java [-options] -jar jarfile [args...] 点击查看获取可执行jar文件方法
- C++中回调函数(CallBack)的使用
如果试图直接使用C++的成员函数作为回调函数将发生错误,甚至编译就不能通过. 其错误是普通的C++成员函数都隐含了一个传递函数作为参数,亦即“this”指针,C++通过传递this指针给其成员函数从而 ...
- 查看大图 zoomImage
添加引用 <link rel="stylesheet" media="screen" type="text/css" href=&qu ...
- Intent 传数据
Intent作为android重要的组件其重要性不言而喻,这里说说他是怎么传递简单数据和对象 Intent的具体概念就不讲解了!网上有很多的 传递简单的数据(例如String,float等) 传递对象 ...