zoukankan      html  css  js  c++  java
  • codeforces 624B Making a String

    Making a String
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:

    • the i-th letter occurs in the string no more than ai times;
    • the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
    Input

    The first line of the input contains a single integer n (2  ≤  n  ≤  26) — the number of letters in the alphabet.

    The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.

    Output

    Print a single integer — the maximum length of the string that meets all the requirements.

    Examples
    input
    3
    2 5 5
    output
    11
    input
    3
    1 1 2
    output
    3
    Note

    For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".

    题意:你可以用n个字母来构成一个字符串,然后给你n个值ai代表分别对应的第i个字符使用次数不得超过ai且所有字母出现的次数都不得相同,求最长的字符串长度

    题解:用一个数组记录使用过的字母出现的次数,重复的次数不可使用

    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<math.h>
    #include<algorithm>
    #define LL long long
    #define PI atan(1.0)*4
    #define DD double
    #define MAX 100100
    #define mod 100
    #define dian 1.000000011
    #define INF 0x3f3f3f
    using namespace std;
    LL s[MAX];
    LL vis[30];//记录使用过的字母的使用次数 
    bool cmp(int a,int b)
    {
    	return a>b;
    }
    int main()
    {
        int n,j,i,t,k,l;
        LL m;
    	while(scanf("%d",&n)!=EOF)
    	{
    		for(i=0;i<n;i++)
    		    scanf("%lld",&s[i]);
    		LL sum=0;k=0;
    		for(i=0;i<n;i++)
    		{
    			m=s[i];
    			sort(vis,vis+k,cmp);
    			for(j=0;j<k;j++)
    			{
    				if(m==0)  
    					break;
    				if(m==vis[j])//判断当前字母可出现的次数是否被使用过 
    					m--;
    			}
    			vis[k++]=m;//更新使用过的次数 
    			sum+=m;
    		}
    		printf("%lld
    ",sum);
    	} 
    	return 0;
    }
    

      

  • 相关阅读:
    WinCE NAND flash
    正确选择报表工具的十大标准
    从技术岗位走向管理岗位:机会是留给有准备的人
    创业失败的七个原因及解决之道
    技术人员如何参与产品设计讨论:激活那一潭死水
    基于Android Studio搭建hello world工程
    基于Android Studio搭建Android应用开发环境
    JS数组去重的6种算法实现
    八款前端开发人员更轻松的实用在线工具
    海市蜃楼-嘉兴外蒲岛奇遇
  • 原文地址:https://www.cnblogs.com/tonghao/p/5239398.html
Copyright © 2011-2022 走看看