zoukankan      html  css  js  c++  java
  • 2019/4/20周赛 F题 CodeForces

    2019/4/20周赛 F题

    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个数字每个数字都要使其不相同,求其sum(求和)最大值。

    #include<cmath>
    #include <iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<vector>
    using namespace std;
    typedef long long LL;
    
    int main()
    {
    	int n,flag=0;
    	LL a[30], sum=0 ;
    	cin >> n;
    	for (int i = 0; i < n; i++)
    	{
    		cin >> a[i];
    	}
    	sort(a, a + n);
    	sum = a[n - 1];
    	for (int i = n - 2; i >= 0; i--)
    	{
    		while (a[i] >= a[i + 1])//>=是精髓能够使其推下去
    			a[i]--;
    		if (a[i] < 0)
    			a[i] = 0;
    		sum += a[i];
    	}
    	cout << sum << endl;
        return 0;
    }
    
  • 相关阅读:
    25 自动类型转换
    24 枚举Enum类
    23 Collection集合常用方法讲解
    Eclipse 快捷键
    21 泛型
    20 IO流(十七)——Commons工具包,FileUtils(二)——copy方法
    19 IO流(十六)——Commons工具包,FileUtils(一)
    18 IO流(十五)——RandomAccessFile随机访问文件及使用它进行大文件切割的方法
    CentOS6.5-6.9安装 docker
    linux开启端口
  • 原文地址:https://www.cnblogs.com/gidear/p/11773646.html
Copyright © 2011-2022 走看看