【题目链接】:http://codeforces.com/contest/509/problem/C

【题意】



给你一个数组b[i]

要求一个严格升序的数组a[i];

使得a[i]是b[i]各个位上的数的和;

并且a[n]最小;

【题解】



每次处理的时候;

算出b[i]-b[i-1]的值

设为d

如果d>0

则从个位开始,不断地加1,直到d变成0;

如果个位变成9了,就加到十位,(这个时候个位的数字9不变,因为在低位,数字大一些,可以为后面高位的数字“分压”,后面的数字就能小一些了;这样整体数字就是最小的了;)然后十位如果小于9就加十位,…以此类推加百位,千位。。

如果d<=0

就先尝试减小数字,让d变成大于0的;

从个位开始;

把数字改为0->(这样满足数字最小,然后在一个适当的高位进一位,就会比前一个数字大了);

然后d+=这个位原来的数字;

直到d大于0;

然后这时记扫描到的位为第i位;

则在这一位i数字加一,然后d减小1;(如果这一位是9,那么再把d加上9然后找到一个<9的位置,在那一位再进位)

然后对于当前的数字;

相当于再加上d个数字,然后数字尽可能小;

这又变成d>0时的情况了;



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,0,-1,0,-1,-1,1,1};
const int dy[9] = {0,0,-1,0,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 300+50; int n,b[N],len;
int digits[10000]; void nex(int rest)
{
for (int i = 1;rest;i++)
{
if (len<i) len = i;
while (rest && digits[i]<9)
{
rest--;
digits[i]++;
}
}
} int main()
{
//freopen("D:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
rei(n);
rep1(i,1,n)
rei(b[i]);
rep1(i,1,n)
{
int d = b[i]-b[i-1];
if (d>0)
{
nex(d);
}
else
{
for (int i = 1; ;i++)
{
if (i>len) len = i;
if (d>0 && digits[i]<9)
{
d--;
digits[i]++;
nex(d);
break;
}
d+=digits[i];
digits[i] = 0;
}
}
rep2(i,len,1)
pri(digits[i]);
pri(endl);
}
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【codeforces 509C】Sums of Digits的更多相关文章

  1. 【81.37%】【codeforces 734B】Anton and Digits

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. [codeforces 509]C. Sums of Digits

    [codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...

  4. 【66.47%】【codeforces 556B】Case of Fake Numbers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【52.49%】【codeforces 556A】Case of the Zeros and Ones

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【42.59%】【codeforces 602A】Two Bases

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【63.73%】【codeforces 560A】Currency System in Geraldion

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【39.29%】【codeforces 552E】Vanya and Brackets

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【33.33%】【codeforces 552B】Vanya and Books

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. [Swift通天遁地]二、表格表单-(7)电子邮件Mail:实现单元格左右滑动调出功能按钮

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. C语言编译器为什么能够用C语言编写?

    不知道大家有没有想过一个问题:C语言编译器为什么能够用C语言编写? 所谓C语言编译器,就是把编程得到的文件,比如.c,.h的文件,进行读取,并对内容进行分析,按照C语言的规则,将其转换成cpu可以执行 ...

  3. SQL数据库还原的二种方式和区别

    1.数据库还原 在SQL中,直接选择选择“还原数据库”:选中.bak 文件即可. 2.生成脚本 新建同样的DB名字,在SQL打开脚本,执行脚本语言.数据库里面就会自动填充内容.

  4. 350 Intersection of Two Arrays II 两个数组的交集 II

    给定两个数组,写一个方法来计算它们的交集.例如:给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].注意:       输出结果中每个元素出现的次数, ...

  5. [转]如何借助 TLS/SSL 确保套接字连接的安全(使用 C#/VB/C++ 和 XAML 的 Windows 应用商店应用)

    本文转自:http://msdn.microsoft.com/zh-cn/library/windows/apps/jj150597.aspx 本主题将展示在使用 StreamSocket 功能时,如 ...

  6. Ubuntu16安装jdk8配置Tomcat9

    一.配置jdk 1.下载解压是肯定不能少的 2.配置环境变量根据自己需求来 export JAVA_HOME=/usr/software/jdk1.8.0_121 export CLASSPATH=. ...

  7. 01--SQLite学习网站参考

    1.   SQLite基本操作 见我的02—SQLite一步到位 sqlite3编程笔记 . http://blog.csdn.net/wl_haanel/article/details/623141 ...

  8. 移动web——bootstrap栅格系统

    基本简介 1.Bootstrap 提供了一套响应式.移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列 2.栅格系统用于通过一系列的行(row)与列(c ...

  9. JS——祝愿墙

    注意事项: 1.for循环的下一层注册了事件的话,事件函数中关于变量i的节点元素是不允许出现的,因为在函数加载的时候,只会加载函数名,不会加载函数体,外层for循环会走完一边,变量i一直会停留在最后一 ...

  10. CSS固定布局:960GS

    960栅格系统 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...