cf848a

http://www.elijahqi.win/archives/626
A. From Y to Y
time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

From beginning till end, this message has been waiting to be conveyed.

For a given unordered multiset of n lowercase English letters (“multi” means that a letter may appear more than once), we treat all letters as strings of length 1, and repeat the following operation n - 1 times:

Remove any two elements s and t from the set, and add their concatenation s + t to the set.
The cost of such operation is defined to be , where f(s, c) denotes the number of times character c appears in string s.

Given a non-negative integer k, construct any valid non-empty set of no more than 100 000 letters, such that the minimum accumulative cost of the whole process is exactly k. It can be shown that a solution always exists.

Input
The first and only line of input contains a non-negative integer k (0 ≤ k ≤ 100 000) — the required minimum cost.

Output
Output a non-empty string of no more than 100 000 lowercase English letters — any multiset satisfying the requirements, concatenated to be a string.

Note that the printed string doesn’t need to be the final concatenated string. It only needs to represent an unordered multiset of letters.

Examples
Input
12
Output
abababab
Input
3
Output
codeforces
Note
For the multiset {‘a’, ‘b’, ‘a’, ‘b’, ‘a’, ‘b’, ‘a’, ‘b’}, one of the ways to complete the process is as follows:

{“ab”, “a”, “b”, “a”, “b”, “a”, “b”}, with a cost of 0;
{“aba”, “b”, “a”, “b”, “a”, “b”}, with a cost of 1;
{“abab”, “a”, “b”, “a”, “b”}, with a cost of 1;
{“abab”, “ab”, “a”, “b”}, with a cost of 0;
{“abab”, “aba”, “b”}, with a cost of 1;
{“abab”, “abab”}, with a cost of 1;
{“abababab”}, with a cost of 8.
The total cost is 12, and it can be proved to be the minimum cost of the process.

由于这道题其实和怎么连接没有关系,判定应该也是spj 所以我们可以随意凑就好了

关于每个字母对于题目要求k值的贡献就是他在集合中出现的次数的一个递减的加法,出现次数*(出现次数-1)/2

With several experiments, you may have found that the “minimum cost” doesn’t make sense — the cost is always the same no matter how the characters are concatenated. Precisely, the cost of the process for a multiset of c1 a’s, c2 b’s, … and c26 z’s, is . It’s in this form because every pair of same characters will contribute 1 to the total cost.

Therefore we need to find such c1, c2, …, c26 so that . This can be done greedily and iteratively. Every time we subtract the maximum possible from k, and add c same new letters to the set, until k becomes 0. This c can be solved by any reasonable way, say quadratic formula, binary search or brute force. Time complexity veries from to or any acceptable complexity, depending on the choice of the method for finding c.

Of course, if a knapsack algorithm is used, it will use the minimum possible number of different letters, and works in .

#include<cstdio>
int k;
int main(){
    freopen("cf.in","r",stdin);
    scanf("%d",&k);
    for (int i=0;i<26;++i){
        int tmp=1;
        while (((tmp*(tmp+1))>>1)<=k) tmp++;
        k-=(tmp*(tmp-1))>>1;
        for (int j=0;j<tmp;++j) printf("%c",'a'+i);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值