Codeforces 849 C From Y to Y 思维

本文提供了一道CodeForces C题目的详细解答过程。题目要求构造一个字符串,其成本等于字符串中每对不同元素共现次数的乘积之和。文章通过分析给出了解题策略,并提供了完整的代码实现。

  题目链接: http://codeforces.com/contest/849/problem/C

  题目描述: 给你个一个cost, 让你构造花费为cost的字符串, 每次的 cost = 两个元素公共元素个数乘积

  解题思路: 很明显发现仅仅一个字母可以构造1, 3, 6, 10......两个不同的字符不会得到cost, 所以我们就先构造一种字母, 不行的用下一个字母填充就可以了......

  代码: 

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <stack>
#include <deque>
#include <map>
#include <set>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define sca(x) scanf("%d",&x)
#define de printf("=======\n")
typedef long long ll;
using namespace std;

int main() {
    int n;
    cin.sync_with_stdio(false);
    string str;
    while( cin >> n ) {
        char c = 'a';
        int num;
        str = "";
        if( n == 0 ) {
            cout << "a" << endl;
            continue;
        }
        while( n ) {
            num = 0;
            while( num <= n ) {
                n -= num;
                str += c;
                num++;
            }
            c++;
        }
        cout << str << endl;
    }
    return 0;
}
View Code

  思考: 我一直不知道为什么, 看起来很显眼的东西我却总是想不到......我打算以后接触一下思维题, 心态炸了......但是还是要控制一下

转载于:https://www.cnblogs.com/FriskyPuppy/p/7468761.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值