codeforces 335A Banana(贪心)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
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".
分析:贪心,先取一遍所有的字母,每次优先去当前所需最多的块数的字母。
- //#####################
- //Author:fraud
- //Blog: http://www.cnblogs.com/fraud/
- //#####################
- #include <iostream>
- #include <sstream>
- #include <ios>
- #include <iomanip>
- #include <functional>
- #include <algorithm>
- #include <vector>
- #include <string>
- #include <list>
- #include <queue>
- #include <deque>
- #include <stack>
- #include <set>
- #include <map>
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- #include <cstring>
- #include <climits>
- #include <cctype>
- using namespace std;
- #define XINF INT_MAX
- #define INF 0x3FFFFFFF
- #define MP(X,Y) make_pair(X,Y)
- #define PB(X) push_back(X)
- #define REP(X,N) for(int X=0;X<N;X++)
- #define REP2(X,L,R) for(int X=L;X<=R;X++)
- #define DEP(X,R,L) for(int X=R;X>=L;X--)
- #define CLR(A,X) memset(A,X,sizeof(A))
- #define IT iterator
- typedef long long ll;
- typedef pair<int,int> PII;
- typedef vector<PII> VII;
- typedef vector<int> VI;
- int num[];
- char str[];
- struct node{
- int num,t;
- char a;
- node(int _num,char _a){
- num=_num,a=_a,t=;
- }
- friend bool operator<(node y,node x){
- return (y.num+y.t-)/y.t < ((x.num+x.t-)/x.t);
- }
- };
- int main()
- {
- ios::sync_with_stdio(false);
- string s;
- int n;
- cin>>s;
- cin>>n;
- int len=s.length();
- int tot=;
- for(int i=;i<len;i++)num[s[i]]++;
- priority_queue<node> q;
- int now=;
- for(int i=;i<;i++){
- if(num[i]){
- str[now++]=i;
- q.push(node(num[i],i));
- }
- }
- if(now>n){
- cout<<-<<endl;
- return ;
- }
- for(;now<n;){
- node p=q.top();
- q.pop();
- str[now++]=p.a;
- p.t++;
- q.push(p);
- }
- node p=q.top();
- int ans=(p.num+p.t-)/p.t;
- cout<<ans<<endl;
- cout<<str<<endl;
- return ;
- }
codeforces 335A Banana(贪心)的更多相关文章
- CodeForces - 158B.Taxi (贪心)
CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...
- 【codeforces 335A】Banana
[题目链接]:http://codeforces.com/contest/335/problem/A [题意] 让你构造一个长度为n的字符串; 每次你可以从这个字符串中任意取走字符; 让你求出取的次数 ...
- codeforces 724D(贪心)
题目链接:http://codeforces.com/contest/724/problem/D 题意:给定一个字符串和一个数字m,选取一个一个子序列s,使得对于字符串中任意长度为m的子序列都至少含有 ...
- Codeforces 626G Raffles(贪心+线段树)
G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...
- Cut 'em all! CodeForces - 982C(贪心dfs)
K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...
- CodeForces - 940E - Cashback +贪心+DP
传送门:CodeForces - 940E - Cashback 题意:在一个长度为n的数组中,可以分出长度为 k 连续的多个数组b(每个数组 b 的 k 可不相同),然后,可以对每个数组 b 进行删 ...
- Codeforces 515C 题解(贪心+数论)(思维题)
题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...
- CodeForces 485C Bits[贪心 二进制]
C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...
- codeforces 732E(贪心)
题目链接:http://codeforces.com/contest/732/problem/E 题意:有n台计算机,m个插座,每台计算机有一个值a[i],每个插座有一个值b[i],每个插座最多只能对 ...
随机推荐
- Codeforces round #353div2 C
题目来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=117863#problem/C 题目大意:给你n个数字,代表这个人在n个银行里面 ...
- Cocos2d-x 3.0 场景切换
场景切换要用到导演类Director,一般有两种方式,大多数是用替换场景(replaceScene),也可以用进栈(pushScene)出栈(popScene)的方式进行场景的替换. 场景切换代码: ...
- [php] PHP创建指定目录和文件
前几天看到有人问PHP环境下如何创建文件到指定目录下,正好自己最近在学习,经过一翻测试,终于出结果了,贴出来与大家分享. 目录结构: 代码所在的文件wwwroot/mydir/test/test.ph ...
- 图形用户界面入门:EasyGui - 零基础入门学习Python035
图形用户界面入门:EasyGui 让编程改变世界 Change the world by program 今天我们来谈谈图形用户界面编程,也就是我们常说的GUI(Graphical User Inte ...
- 双人贪吃蛇小游戏C++原创
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang /*贪吃蛇*/ #include<stdio.h> #include<time. ...
- Autolayout-VFL语言添加约束-备
一.VFL语言简介 VFL(Visual format language)语言是苹果为了简化手写Autolayout代码所创建的专门负责编写约束的代码.为我们简化了许多代码量. 二.使用步骤 使用步骤 ...
- Java开发工具与程序调试
开发工具:MyEclipse,Eclipse等. 程序调试: (1)断点:设置断点是程序调试中必不可少的手段,Java调试器每次遇到程序断点时都会将当前线程挂起,即暂停当前程序的运行.(在Eclip ...
- 完美解决ListView 与 ScrollView 共存问题
1:首先设置ListView的高度,在setAdapter之后调用此方法. public static void setListViewHeightBasedOnChildren(ListView l ...
- Buffer lock
buffer lock Oracle 提供非常精确,有效的Row Level Lock机制,多个用户同时修改数据时,为了保护数据. 以块为单位挂起锁的情况不会发生,但这不太正确. 以块为单位的锁 ...
- spring 4 泛型注入
最近对系统进行改造,发现在泛型实例初始化的时候,得不到想要的泛型.或者需要强制转换. spring 4 开始支持泛型对象初始化,初始化方法如下: 注:使用配置文件的方法暂时还没有发现,下面是使用jav ...