【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 ...
随机推荐
- Web Tab, Project Properties
https://msdn.microsoft.com/en-us/library/aa983445(v=vs.100).aspx The Web tab of the project Properti ...
- YTU 2732:3798-Abs Problem
2732: 3798-Abs Problem 时间限制: 1 Sec 内存限制: 128 MB Special Judge 提交: 167 解决: 60 题目描述 Alice and Bob i ...
- E20170629-hm
enqueue [计] 入队,排队; dequeue [计] 出列; rear n. 后部,背面,背后; 臀部; (舰队或军队的) 后方,后尾,殿后部队; 〈英口〉厕所; ring buffe ...
- 高德,百度,Google地图定位偏移以及坐标系转换
一.在进行地图开发过程中,我们一般能接触到以下三种类型的地图坐标系: 1.WGS-84原始坐标系 一般用国际GPS纪录仪记录下来的经纬度,通过GPS定位拿到的原始经纬度,Google和高德地图定位的的 ...
- Sorting It All Out 拓扑排序+确定点
这一道题的话 数据有一点问题 ........ 例如 不过 还是 能理解一下 试试吧 ......... A<B B<C C<A A<C B<A ...
- JavaScript--编程
第一步:把注释语句注释. 第二步:编写代码,在页面中显示 “系好安全带,准备启航--目标JS”文字: 第三步:编写代码,在页面中弹出提示框“准备好了,起航吧!” 提示: 可以把弹框方法写在函数里. 第 ...
- 用CSS样式写选择框右侧小三角
直接上代码! <!DOCTYPE html><html lang="en"><head> <title>小三角</title& ...
- Angular——内置指令
内置指令 ng-app 指定应用根元素,至少有一个元素指定了此属性. ng-controller 指定控制器 ng-show控制元素是否显示,true显示.false不显示 ng-hide控制元素是否 ...
- html5——多媒体(二)
基本方法 load() //重新加载视频 play() //播放 pause() //暂停 基本属性 currentTime //视频播放的当前进度. duration //视频的总时间 paused ...
- C# 定时无操作则退出登陆,回到登陆界面。
有时候根据需求需要为程序添加在规定的时间内无操作则退出当前的登陆程序的功能,如下代码模拟描述的需求功能. using System; using System.Collections.Generic; ...