zoukankan      html  css  js  c++  java
  • Codeforces 509C Sums of Digits

    http://codeforces.com/contest/509/problem/C

     题目大意:

    给出一个序列,代表原序列对应位置数的每一位的数字之和,原序列单调递增,问原序列的最后一个数最小的方案每一个数是多少。

    思路:贪心,从1到n,我们尽量让每个数最小就可以了。

     1 #include<cstdio>
     2 #include<cmath>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<iostream>
     6 int g[200005],n,a[200005];
     7 int read(){
     8     int t=0,f=1;char ch=getchar();
     9     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    10     while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();}
    11     return t*f;
    12 }
    13 void work(int x,int y){
    14     if (x==y){
    15         g[1]++;
    16         int i=1;
    17         while (i<=g[0]||g[i]>9){
    18             g[i+1]+=g[i]/10;
    19             g[i]%=10;
    20             i++;
    21         }
    22         while (i>1&&g[i]==0) i--;
    23         g[0]=i;
    24         x=0;
    25         for (int i=1;i<=g[0];i++)
    26          x+=g[i];
    27     }
    28     if (x>y){
    29         int j=0,i;
    30         for (i=1;i<=g[0]&&x-j>=y;i++) j+=g[i];
    31         while (g[i]==9) i++;
    32         for(g[i--]++;i;i--) g[i]=0;
    33         int k=1;
    34         while (k<=g[0]||g[k]>9){
    35             g[k+1]+=g[k]/10;
    36             g[k]%=10;
    37             k++;
    38         }
    39         while (k>1&&g[k]==0) k--;
    40         g[0]=k;
    41         x=0;
    42         for (int i=1;i<=g[0];i++)
    43          x+=g[i];
    44     }
    45     int i;
    46     for (i=1;x<y;i++)
    47      if (9-g[i]+x>=y) g[i]+=y-x,x=y;
    48          else x+=9-g[i],g[i]=9;
    49     if (g[0]<i-1) g[0]=i-1;     
    50 }
    51 int main(){
    52     n=read();
    53     for (int i=1;i<=n;i++) a[i]=read();
    54     for (int i=1;i<=n;i++){
    55         work(a[i-1],a[i]);
    56         for (int j=g[0];j>=1;j--)
    57          printf("%d",g[j]); 
    58         puts("");  
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    python主成分分析
    matplotlib绘图pie
    cpu监控:mpstat命令
    cpu监控:dstat
    MongoDB--安装部署
    Linux-网络管理
    Apache 虚拟主机配置
    Apache 访问控制
    Apache 域名跳转配置
    Apache 日志管理
  • 原文地址:https://www.cnblogs.com/qzqzgfy/p/5630012.html
Copyright © 2011-2022 走看看