这是我的第一次打TC,感觉打的一般般吧。不过TC的题目确实挺有意思的。

由于是用客户端打的,所以就不发题目地址了。

300分的题:

这题大意是有一段序列只包含+和数字0~9

一段序列的操作是,从头扫到尾,遇到+就对计数器+1。遇到数字就计算abs(num-count)的值,并加到sum中。

题目要求重新排序序列,使得sum最小。

由于是abs,最小值自然是0,于是就是要构造0

由于计数器只会网上增,所以数字我肯定从小到大排,于是对于某个数,计数器不足这个数,那么就需要增加计数器,否则就计算abs

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long

using namespace std;

class Plusonegame
{
    public:
    string getorder(string s)
    {
        int cnt[11] = {0}, all = 0;
        string ans = "";
        for (int i = 0; i < s.length(); ++i)
        {
            if (s[i] == '+') cnt[10]++;
            else cnt[s[i]-'0']++;
        }
        for (int i = 0; i < 10; ++i)
        {
            while (all < i && cnt[10])
            {
                all++;
                cnt[10]--;
                ans += '+';
            }
            while (cnt[i])
            {
                ans += i+'0';
                cnt[i]--;
            }
        }
        while (cnt[10])
        {
            ans += '+';
            cnt[10]--;
        }
        return ans;
    }
};
View Code

相关文章: