zoukankan      html  css  js  c++  java
  • CF 335A(Banana-贪心-priority_queue是大根堆)

    A. Banana
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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.

    Input

    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).

    Output

    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.

    Sample test(s)
    input
    banana
    4
    
    output
    2
    baan
    
    input
    banana
    3
    
    output
    3
    nab
    
    input
    banana
    2
    
    output
    -1
    
    Note

    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
    ",ans+1);
    	}
    	
    	
    	
    	return 0;
    }
    






  • 相关阅读:
    idea编译eclipse项目时修改java代码后运行不生效
    IDEAL启动项目的时候报java.lang.NoClassDefFoundError: javax/servlet/Filter错误
    springboot项目运行报错Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
    Error:(5, 25) java: 程序包javax.servlet.jsp不存在
    未载入,因为它的 MIME 类型 "text/html" 不是 "text/css"
    Win10下PHP加载php8_module报错“Can‘t locate API module structure `php8_module‘ in file XXX“解决方法供参考
    apache启动服务报错ServerRoot must be a valid directory
    如何从Apache官网下载windows版apache服务器
    解决request.getServletContext()方法报红问题
    [Cloud DA] Serverless Framework with AWS
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3236876.html
Copyright © 2011-2022 走看看