CF848A From Y to Y【思路】

题意:给一个费用k,输出一个字符串,合并这个字符串的最小费用为k,费用为:一个字母在两个字符串中出现的次数的乘的累加和。


思路:不同字母合并不需要费用,只含一个字母的字符串长度为c,我们发现它的费用为c*(c-1)/2。总费用就是不同长度含不同字母的字符串的费用和,每次给当前字母长度+1,超过费用,就回到上一次位置,换一个字母继续。费用为0,需要特判。


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<list>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1e6+5;
char ans[maxn];
int tot;

int main(void)
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		tot = 0;
		int sum = 0,res = 0,last = 0,now = 0;
		while(sum != n)
		{
			now++;
			if(now >= 2)
				last = now*(now-1)/2 - (now-2)*(now-1)/2;
			else
				last = now*(now-1)/2;
			sum += last;
			ans[tot++] = 'a' + res;
			if(sum > n)
			{
				res++;
				tot--;
				sum -= last;
				last = 0;
				now = 0;
			}
		}
		ans[tot] = '\0';
		if(n != 0)
			printf("%s\n",ans);
		else
			printf("a\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值