【codeforces 509C】Sums of Digits
【题目链接】: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的更多相关文章
- 【81.37%】【codeforces 734B】Anton and Digits
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- [codeforces 509]C. Sums of Digits
[codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...
- 【66.47%】【codeforces 556B】Case of Fake Numbers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【52.49%】【codeforces 556A】Case of the Zeros and Ones
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【42.59%】【codeforces 602A】Two Bases
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【63.73%】【codeforces 560A】Currency System in Geraldion
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【33.33%】【codeforces 552B】Vanya and Books
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- Linux中grep命令的12个实践例子
grep是每个Linux发行版都预装的一个强有力的文件模式搜索工具.无论何种原因,如果你的系统没有预装它的话,你可以很容易的通过系统的包管理器来安装它(Debian/Ubuntu系中的apt-get和 ...
- 在Java中实现UDP协议编程(DatagramSocket/DatagramPacket)
1.什么是UDP协议? UDP( User Datagram Protocol )协议是用户数据报,在网络中它与TCP协议一样用于处理数据包.在OSI模型中,在第四层——传输层,处于IP协议的上一层. ...
- cas4.2的安装
cas4.2使用的是gradle来构建项目的,项目代码在https://github.com/Jasig/cas下载. 然后之后进入项目的根目录,然后执行gradle来编译项目,如下: gradle ...
- [Apple开发者帐户帮助]七、注册设备(3)禁用或启用设备
您可以禁用和启用设备,但不能从开发者帐户中删除它.您可以禁用不再用于开发或测试的设备.但是,这样做会使包含设备的配置文件无效,并且不会增加当年设备的总数. 所需角色:帐户持有人或管理员. 在“ 证书” ...
- memcache缓存系统
一.缓存系统 静态web页面: 1.在静态Web程序中,客户端使用Web浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Request),告诉服 ...
- Java高质量20问
问题一:在多线程环境中使用HashMap会有什么问题?在什么情况下使用get()方法会产生无限循环? HashMap本身没有什么问题,有没有问题取决于你是如何使用它的.比如,你在一个线程里初始化了一个 ...
- $luogu2375[NOI2014]$
\(problem\) 其中,\(next[i],next[next[i]],next[next[next[i]]]......\)都是这个前缀串i的公共前后缀,而且只有它们是公共前后缀 那么,我们其 ...
- 实战篇之实现 OutLook 中以 EDM 形式发送通知邮件
1.写 Html5 的 EDM 模板 EDM 源代码示例: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ...
- Android开发之ThreadLocal原理深入理解
[Android]ThreadLocal的定义和用途 ThreadLocal用于实现在不同的线程中存储线程私有数据的类.在多线程的环境中,当多个线程需要对某个变量进行频繁操作,同时各个线程间不需要同步 ...
- c++ 以二进制和以文本方式读写文件的区别
在c++项目开发中,时常涉及到文件读写操作.因此在这里先简单梳理和回顾一下文本模式和二进制模式在进行文件读写上的区别. 1.linux平台下文本文件和二进制文件的读写 在linux平台下进行文件读写时 ...