codeforces 624B Making a String
1 second
256 megabytes
standard input
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.
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.
Print a single integer — the maximum length of the string that meets all the requirements.
3
2 5 5
11
3
1 1 2
3
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\n",sum);
}
return 0;
}
codeforces 624B Making a String的更多相关文章
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- 【动态规划】【最短路】Codeforces 710E Generate a String
题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...
- codeforces 632C The Smallest String Concatenation
The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...
- 【Codeforces 1120C】Compress String
Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...
- 【codeforces 797C】Minimal string
[题目链接]:http://codeforces.com/contest/797/problem/C [题意] 一开始,给你一个字符串s:两个空字符串t和u; 你有两种合法操作; 1.将s的开头字符加 ...
- Codeforces 1144 E. Median String
原题链接:https://codeforces.com/problemset/problem/1144/E tag:字符串模拟,大整数. 题意:给定两个字符串,求字典序中间串. 思路:可以把这个题当做 ...
- CodeForces 710E Generate a String (DP)
题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...
- Codeforces 56D Changing a String
http://codeforces.com/contest/56/problem/D 题目大意: 一个字符串变为目标字符串,可以执行插入,置换和删除3种操作,求最少操作数. 思路:dp[i][j]代表 ...
- codeforces 632C. The Smallest String Concatenation 排序
题目链接 给出n个字符串, 将他们连在一起, 求连玩之后字典序最小的那种情况. 按a+b<b+a排序.... #include <iostream> #include <vec ...
随机推荐
- C语言中的宏展开
#include<stdio.h> #define f(a,b) a##b #define g(a) #a #define h(a) g(a) int main() { printf(,) ...
- JEE学习线路
传智播客:javaEE学习线路以及需要掌握的知识点:http://java.itcast.cn/subject/javastudypath/index.shtml 最近在学JavaEE,没学Java ...
- WebClient+Fiddler2完美搭配下载远程页面信息
WebClient可以下载远程页面信息,这个大家应该都知道,核心代码如下: WebClient web = new WebClient(); string url = String.Format(&q ...
- sql Server 的基本函数
--聚合函数 use pubs go select avg(distinct搜索 price) --算平均数 from titles where type='business' go use pubs ...
- MySQL Timeout解析
“And God said, Let there be network: and there was timeout”在使用MySQL的过程中,你是否遇到了众多让人百思不得其解的Timeout?那么这 ...
- ASP.NET线程相关配置
1.(maxWorkerThreads * CPU逻辑数量)-minFreeThreads 比如2个CPU默认配置maxWorkerThreads=100,minFreeThreads=176,则同时 ...
- Java [Leetcode 260]Single Number III
题目描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...
- Go2Shell 打开设置窗口
Go2Shell默认没有设置界面,需要用命令行打开 在Shell中输入: open -a Go2Shell --args config
- Spring学习之基本概念
Spring 基本概念 Spring优点: 1.Spring不同于其它的Framework,它要提供的是一种管理你的业务对象的方法. 2.DI有效的降低了耦合度 3.AOP提供了通用任务的集中管理 4 ...
- 【转】cocos2d-x Lua
Call custom c++ from Lua cocos2d-x lua binds c++ class, class functions ,enum and some global functi ...