Codeforces Round #289 (Div. 2, ACM ICPC Rules) C. Sums of Digits
来源:互联网 发布:mac如何ping ip地址 编辑:程序博客网 时间:2024/05/21 06:57
Vasya had a strictly increasing sequence of positive integers a1, ..., an. Vasya used it to build a new sequence b1, ..., bn, where bi is the sum of digits of ai's decimal representation. Then sequence ai got lost and all that remained is sequence bi.
Vasya wonders what the numbers ai could be like. Of all the possible options he likes the one sequence with the minimum possible last number an. Help Vasya restore the initial sequence.
It is guaranteed that such a sequence always exists.
The first line contains a single integer number n (1 ≤ n ≤ 300).
Next n lines contain integer numbers b1, ..., bn — the required sums of digits. All bi belong to the range 1 ≤ bi ≤ 300.
Print n integer numbers, one per line — the correct option for numbers ai, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the i-th number should be equal to bi.
If there are multiple sequences with least possible number an, print any of them. Print the numbers without leading zeroes.
3123
123
3321
311100
题意很简单,给n个数字,要求n个数单调递增,且每个数的数字和为对应的这n个数!
这题150行的代码写了1个多小时,太差了,原理,就是从找比第n个数大1的数且位数和为第i个原始位数和pri[i],如果当前要求的位数和比上一位位数和要大,则尽量从右向左每一位加到9,再向前加就得出结果,如果,当前要求的位数比上一位位数和要小,则尽量把底位置为0,还是达不到的话,说明比第一位
还要小,就直接向前进一位就可以了,总位数不会超过300个,因为最大值也才为300!
#define INF9000000000#define EPS(double)1e-9#define mod1000000007#define PI3.14159265358979//*******************************************************************************/#endif#define N 305#define M 1005#define maxn 205#define MOD 1000000000000000007int n,pri[N],goal[M],goallen,goalNum,ans[M],anslen,ansNum;void FindNext(){ int sum = 0,nextNum ; anslen = 0; if(ansNum == goalNum){ anslen = goallen; FI(goallen) ans[i] = goal[i]; } else if(ansNum > goalNum){ nextNum = ansNum - goalNum; FI(goallen){ sum = 9-goal[i]; if(nextNum <= sum){ ans[i] = goal[i] + nextNum; anslen = goallen; for(int j = i+1;j<anslen;j++){ ans[j] = goal[j]; } nextNum = 0; break; } else { nextNum -= sum; ans[i] = 9; } } anslen = goallen; while(nextNum > 0){ if(nextNum >=9){ ans[anslen++] = 9; nextNum -= 9; } else { ans[anslen++] = nextNum; nextNum = 0; } } } else { bool isFind = false; nextNum = goalNum - ansNum; sum = -1; FI(goallen - 1){ sum+=goal[i]; if(goal[i+1] == 9) continue; if(nextNum <= sum){ anslen = goallen; for(int j = goallen;j>i;j--){ ans[j] = goal[j]; } ans[i+1] = goal[i+1] + 1; for(int j = i;j>=0;j--){ ans[j] = 0; } int temps = 0; for(int k=0;k<anslen;k++){ temps+=ans[k]; } if(temps <= ansNum){ int ansi = 0; temps = ansNum - temps; while(temps > 0){ if(temps >=9){ ans[ansi++] = 9; temps -= 9; } else { ans[ansi++] = temps; temps = 0; } } } isFind = true; break; } } if(!isFind){ anslen = goallen+1; FI(anslen) ans[i] = 0; int tempNum = ansNum,tlen = 0; while(tempNum){ if(tempNum >=9){ ans[tlen++] = 9; tempNum -= 9; } else { ans[tlen++] = tempNum; tempNum = 0; } } if(tlen < anslen){ ans[tlen-1]--; ans[anslen - 1] = 1; } } } goallen = anslen; FI(anslen){ goal[i] = ans[i]; } goal[0]++; FI(anslen){ if(goal[i] >= 10){ if(i == anslen - 1){ goal[i+1] = 0; goallen++; } goal[i+1] += 1; goal[i] %= 10; } } goalNum = 0; FI(goallen){ goalNum += goal[i]; }}int main(){ while(S(n)!=EOF) { FI(n){ S(pri[i]); } memset(goal,0,sizeof(goal)); memset(ans,0,sizeof(ans)); goallen = 1;anslen = 0;goalNum = 0; FI(n){ ansNum = pri[i]; FindNext(); FJ(anslen){ printf("%d",ans[anslen-1-j]); } printf("\n"); } } return 0;}
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)C. Sums of Digits
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)C. Sums of Digits——贪心+模拟
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) C. Sums of Digits
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) C. Sums of Digits
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)C、E
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) C题
- Codeforces Round #116 (Div. 2, ACM-ICPC Rules) C - Letter
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)(A,B)
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) 部分题解
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) (A, B, C, E)
- Codeforces Round #116 (Div. 2, ACM-ICPC Rules) D - Name
- Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E - Cubes
- Codeforces Round #145 (Div. 2, ACM-ICPC Rules)
- Codeforces Round #145 (Div. 2, ACM-ICPC Rules) Fence
- 75.字符串正序和反序连接
- 解析如何改善和优化 Web 服务器性能
- retain的循环引用注意以及@class的使用
- Socket编程 (异步通讯,解决Udp丢包)
- clean code 读书笔记
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) C. Sums of Digits
- 拼包函数及网络封包的异常处理(含代码)
- php常用函数收集并附解释说明
- android动画中fillBefore以及fillAfter在xml中设置无效的问题
- 使用JSONObject和JSONArray
- linux学习笔记---2
- 基于win平台的高性能服务器底层通信模型设计(1)
- 有道云笔记
- 《Java程序》将数字进行倒序输出