【题目链接】: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. 分类(category)是门学问

    分类的精细程度表现了人类的文明程度. 1. 学科分类 cybernetics:控制论:

  2. Ned的难题

    题目描述 Ned再也看不下去Robert的种种恶习,于是他决定出一道题来让他醒悟. Ned的题目是这样: 给出一个有n个数的序列,求其中所有连续子序列的数的最大公因数的乘积模1000000009的值. ...

  3. codeforces round #416 div2

    A:暴力模拟 #include<bits/stdc++.h> using namespace std; int a, b; int main() { scanf("%d%d&qu ...

  4. Linux命令(005) -- kill、pkill和killall的比较

    kill命令用来“杀掉”指定进程PID的进程.终止一个前台进程可以使用Ctrl+C,终止一个后台进程就须用kill命令.kill命令是通过向进程发送指定的信号来结束相应进程的.在默认情况下,kill命 ...

  5. 参加2016华为codecraft编程精英挑战赛后感

    2016年4月参加了华为的软件比赛. 关于比赛:给了一道图论的np-hard问题.刚开始完全不知道怎么入手,请教过师兄,自己也琢磨过,没有什么万全的解决方法.注意,这里说的是万全的办法.本科搞算法时候 ...

  6. [转]linux之cut命令的用法

    转自:http://www.jb51.net/article/41872.htm cut是一个选取命令,就是将一段数据经过分析,取出我们想要的.一般来说,选取信息通常是针对“行”来进行分析的,并不是整 ...

  7. 增加删除div

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  8. JS高级——变量提升

    JS执行过程 1.首先是预解析:预解析过程最重要的是提升,在JavaScript代码在预解析阶段,会对以var声明的变量名,和function开头的语句块,进行提升操作 2.执行操作 全局中解析和执行 ...

  9. Ajax——跨域访问

    同源 基本概念:同源策略是浏览器的一种安全策略,所谓同源是指,域名,协议,端口完全相同. //同一域名下,允许通讯 http://www.a.com/a.js http://www.a.com/b.j ...

  10. java攻城狮之路--复习JDBC(数据库连接池 : C3P0、DBCP)

    复习数据库连接池 : C3P0.DBCP 1.数据库连接池技术的优点: •资源重用:      由于数据库连接得以重用,避免了频繁创建,释放连接引起的大量性能开销.在减少系统消耗的基础上,另一方面也增 ...