CF910 C.Minimum Sum

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF910 C.Minimum Sum相关的知识,希望对你有一定的参考价值。

题目链接:http://codeforces.com/problemset/problem/910/C
题目大意:将a~j分别赋值0~9,然后计算表达式的最小值
一道普通的模拟题,思路在于将a~j的权重计算清楚,然后排序分配0~9,同时注意一下前导零的问题。

#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
char dic[1000][10];
int cnt_appear[15][10];
struct node
{
    int index;
    int rate;
};
int f(int x)
{
    int res = 1;
    for(int i = 1; i <= x; i ++)
    {
        res *=10;
    }
    return res;
}
map<char,int>mymap;
bool cmp(node n1, node n2)
{
    return n1.rate > n2.rate;
}
node rating[15];
bool can_not_zero[15];
int main()
{
    for(int i = 1; i <= 10; i ++)
    {
        rating[i].index = i;
    }
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++)
    {
        scanf("%s", dic[i]);
        can_not_zero[dic[i][0] - ‘a‘ + 1] = 1;
        int temp_len = strlen(dic[i]);
        for(int j = 0; j < temp_len; j ++)
        {
            rating[dic[i][j] - ‘a‘ + 1].rate += f(temp_len - j);
        }
    }
    sort(rating + 1,rating + 11, cmp);
    int flag = 0;
    int rk = 1;
    for(int i = 1; i <= 10; i ++)
    {
        if(flag == 0)
        {
            if(can_not_zero[rating[i].index] == 1)
            {
                char c = rating[i].index + ‘a‘ - 1;
                mymap[c] = rk;
                rk++;
            }
            else
            {
                char c = rating[i].index + ‘a‘ - 1;
                mymap[c] = 0;
                flag = 1;
            }
        }
        else if(flag == 1)
        {
            char c = rating[i].index + ‘a‘ - 1;
            mymap[c] = rk;
            rk++;
        }
    }
    long long sum = 0;
    for(int i = 1; i <= n; i ++)
    {
        long long temp_sum = 0;
        for(int j = 0; j < strlen(dic[i]); j ++)
        {
            temp_sum = temp_sum * 10 + mymap[dic[i][j]];
        }
        sum+=temp_sum;
    }
    printf("%lld\n",sum);
    return 0;
}

以上是关于CF910 C.Minimum Sum的主要内容,如果未能解决你的问题,请参考以下文章

2021牛客暑期多校训练营3 C.Minimum grid(二分图匹配)

1 代码片段1

[题解] CF85D Sum of Medians

CF 914 G Sum the Fibonacci —— 子集卷积,FWT

CF 489 C Given Length and Sum of Digits... 贪心

CF873B Balanced Substring (前缀和)