【CodeForces - 849】C From Y to Y 【思维+构造+贪心】

37 篇文章 0 订阅
25 篇文章 1 订阅

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.

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

题意 构造一个长度为n字符串,让其执行n-1次 如题的操作,每次操作的费用为这里写图片描述
现在给其最小的花费,构造出这样花费的字符串。

分析: 我们可以先单独考虑重复的单独的字母进行操作的花费特点
a 0
aa 1
aaa 3
aaaa 6
aaaaa 10
……
然后我们可以发现 n个重复的字母其花费为(n-1)*n/2 ;
然后我们再将情况复杂点
aaab 3+0
aaabb 3+1
aaabbb 3+3
aaabbbb 3+6
aaabbbbb 3+10
……
这个时候我们就可以发现,其花费是不影响的,重复的只算重复的那部分就可以了。
每个字母的花费规律为
0 1 3 6 10 15 21 28 ……
现在问题变成了,给定一个k,在可以随便用上述所有元素的情况下, 使其和为k。 因为题目没有标明构造不出的情况,所以题目一定可以构造成,故我们可以贪心的每次找到第一个小于k的值,然后输出相应数量的字符,再将k减去相应的值,不断重复下去。直到k为0 。
代码

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 1e4;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

int arr[MAXN]; 
int main(){
    CLOSE();
//  fread();
//  fwrite();
    for(int i=1;i<MAXN;i++) arr[i]=arr[i-1]+i; // 先打表出来

    int k;cin>>k;
    char temp='a';
    if(k==0) putchar(temp);//特判一下
    while(k){
        int cnt=lower_bound(arr,arr+MAXN,k)-arr;
        if(arr[cnt]>k) cnt--;// 找到第一个小于k的值
        k-=arr[cnt];
        cnt++;  // 因为序号为0的时候,是1个a。 
        while(cnt--) putchar(temp);
        temp++;// 改变字符
    } 
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值