zoukankan      html  css  js  c++  java
  • 2020牛客寒假算法基础集训营4 E 最小表达式

    https://ac.nowcoder.com/acm/problem/201596

    贪心:

    1、数字位数尽可能的少,所以尽可能平均分数字长度

    2、数字高位尽可能的小

    所以如果有s个加号,m个1—9的数字,就分成s+1个数字,其中有m%(s+1)个数字位数为m/(s+1)+1,剩余的数字位数为m/(s+1)

    将所有的数字按加法竖式的方式写出来,可以发现位于某个数位的1个1—9的数字,属于哪个数都可以

    所以从9开始从大往小选数字,先把所有的个位数填满,再填十位数,逐渐到最高位

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    #define N 500001
    
    char s[N]; 
    int num[N],ans[N];
    
    int main()
    {
        scanf("%s",s+1);
        int n=strlen(s+1),sum0=0,m=0;
        for(int i=1;i<=n;++i)
            if(s[i]=='+') sum0++;
            else num[++m]=s[i]-'0';
        sort(num+1,num+m+1);
        int len1=m/(sum0+1),sum2=m%(sum0+1),sum1=sum0+1;
        int now=0,len=0;
        for(int i=1;i<=len1;++i)
        {
            for(int j=1;j<=sum1;++j) now+=num[m--];
            ans[++len]=now%10;
            now/=10;
        }
        for(int j=1;j<=sum2;++j) now+=num[m--];
        while(now) ans[++len]=now%10,now/=10;
        for(int i=len;i;--i) printf("%d",ans[i]);
        return 0;    
    }
  • 相关阅读:
    HDU 2276
    HDU 2254
    HDU 1536 & 1944
    HDU 1538
    HDU 2177
    HDU 2176
    HDU 1209
    HDU 1254
    c++ 11 default delete
    ssh免密登录
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/12300368.html
Copyright © 2011-2022 走看看