CodeForces 509C Sums of Digits(贪心乱搞)题解
题意:a是严格递增数列,bi是ai每一位的和,告诉你b1~bn,问你怎样搞才能让an最小
思路:让ai刚好大于ai-1弄出来的an最小。所以直接模拟贪心,如果当前位和前一个数的当前位一样并且后面还能生成比前一个数大的数,那么就和前一个数保持一致,否则当前位 = 前一个数当前位+ 1,后面的位数按照最小方式排列。如果排到最后每一位都和前面一个数一致,就把剩余的b从最小的一位一直加满9。
代码:
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = + ;
const int MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
int b[maxn], ans[maxn][maxn + ], cnt[maxn];
void solve1(int i){
cnt[i] = ;
while(b[i] > ){
ans[i][cnt[i]++] = ;
b[i] -= ;
}
ans[i][cnt[i]++] = b[i];
}
int main(){
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", &b[i]);
} solve1();
for(int i = ; i <= n; i++){
if(b[i] - * cnt[i - ] > ){
solve1(i);
}
else{
if(b[i] <= ans[i - ][cnt[i - ] - ]){
cnt[i] = cnt[i - ] + ;
ans[i][cnt[i] - ] = ;
b[i]--;
for(int j = ; j < cnt[i] - ; j++){
if(b[i] >= ){
ans[i][j] = ;
b[i] -= ;
}
else if(b[i] > ){
ans[i][j] = b[i];
b[i] = ;
}
else ans[i][j] = ;
}
}
else{
cnt[i] = cnt[i - ];
for(int j = cnt[i] - ; j >= ; j--){
if(j != ){
if(b[i] - ans[i - ][j] <= ans[i - ][j - ]){
ans[i][j] = ans[i - ][j] + ;
b[i] -= ans[i][j];
while(ans[i][j] > ){
b[i] += ans[i][j] - ;
j++;
if(j == cnt[i]){
ans[i][j] = ;
cnt[i]++;
}
ans[i][j]++;
} for(int k = ; k < j; k++){
if(b[i] >= ){
ans[i][k] = ;
b[i] -= ;
}
else if(b[i] > ){
ans[i][k] = b[i];
b[i] = ;
}
else ans[i][k] = ;
}
break;
}
else{
ans[i][j] = ans[i - ][j];
b[i] -= ans[i][j];
}
}
else{
ans[i][j] = ans[i - ][j];
b[i] -= ans[i][j];
}
}
if(b[i] > ){
for(int j = ; j < cnt[i] && b[i] > ; j++){
if(b[i] > - ans[i][j]){
b[i] -= - ans[i][j];
ans[i][j] = ;
}
else{
ans[i][j] += b[i];
b[i] = ;
}
}
}
}
}
}
for(int i = ; i <= n; i++){
for(int j = cnt[i] - ; j >= ; j--)
printf("%d", ans[i][j]);
printf("\n");
}
return ;
}
CodeForces 509C Sums of Digits(贪心乱搞)题解的更多相关文章
- Codeforces 509C Sums of Digits 贪心
这道题目有人用DFS.有人用DP 我觉得还是最简单的贪心解决也是不错的选择. Ok,不废话了,这道题目的意思就是 原先存在一个严格递增的Arrary_A,然后Array_A[i] 的每位之和为Arra ...
- Codeforces 509C Sums of Digits
http://codeforces.com/contest/509/problem/C 题目大意: 给出一个序列,代表原序列对应位置数的每一位的数字之和,原序列单调递增,问原序列的最后一个数最小的方 ...
- codeforces 653C C. Bear and Up-Down(乱搞题)
题目链接: C. Bear and Up-Down time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces 723c [贪心][乱搞]
/* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给一个n和m. 第二行给n个数. 每次操作可以把n个数中的任何一个数替代为别的数,问最少的操作次数使得1.2.3.4.5...m中的数出现的次数 ...
- [CF752D]Santa Claus and a Palindrome(优先队列,贪心乱搞)
题目链接:http://codeforces.com/contest/752/problem/D 题意:给长度为k的n个字符串,每一个字符串有权值,求构造一个大回文串.使得权值最大. 因为字符串长度都 ...
- Codeforces Gym 100203G Good elements 暴力乱搞
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以 ...
- [luoguP1053] 篝火晚会(贪心 + 乱搞)
传送门 假设第一个位置是1,那么枚举它的左右两边是谁,有两种情况,然后可以递推求出序列. 然后可以贪心,两个序列有多少个不同的数,答案就是多少,具体为啥,yy一下即可 然后就是判断递推求出的序列和目标 ...
- FJNU2018低程F jq解救fuls (贪心乱搞)题解
题目描述 一天fuls被邪恶的"咕咕咕"抓走了,jq为了救fuls可谓是赴汤蹈火,费了九牛二虎之力才找到了"咕咕咕"关押fuls的地方. fuls被关在一个机关 ...
- CodeForces 81D.Polycarp's Picture Gallery 乱搞
D. Polycarp's Picture Gallery time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
随机推荐
- Rpgmakermv(30) GameQuest任务插件
插件简介: 很牛X的任务插件(个人目前用过中的) 插件用法说明 Report any bugs, editor or plugin related here: http://forums.rpgmak ...
- 互联网级监控系统必备-时序数据库之Influxdb技术
时间序列数据库,简称时序数据库,Time Series Database,一个全新的领域,最大的特点就是每个条数据都带有Time列. 时序数据库到底能用到什么业务场景,答案是:监控系统. Baidu一 ...
- 排序(I)
NSSortDescriptor 排序 Person类 #import <Foundation/Foundation.h> @interface Person : NSObject @pr ...
- 20165305 苏振龙《Java程序设计》第二周学习总结
代码托管(ch2,ch3) 脚本截图 教材内容总结 类型.变量与运算符 基本类型 整数(short.int.long) 字节(byte) 浮点数(float/double) 字符(char)将一个数字 ...
- hive 常见时间日期函数的使用
1.时间戳函数 日期转时间戳:从1970-01-01 00:00:00 UTC到指定时间的秒数 获得当前时区的UNIX时间戳: select unix_timestamp(); 1533716607 ...
- JAVA程序测试感受
上周四下午,我们进行了JAVA测试,心里很慌,在家中只是学习了JAVA程序的输入.输出以及各种数据类型使用而已,王建民老师给我们发了一份JAVA的课前测试样卷,是关于学生信息管理系统的,我们提前从学长 ...
- php ci nginx 伪静态rewrite配置方法
php ci nginx 伪静态rewrite配置方法 location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$ ...
- Python+OpenCV图像处理(九)—— 模板匹配
百度百科:模板匹配是一种最原始.最基本的模式识别方法,研究某一特定对象物的图案位于图像的什么地方,进而识别对象物,这就是一个匹配问题.它是图像处理中最基本.最常用的匹配方法.模板匹配具有自身的局限性, ...
- Django之连接远程mysql数据库
1.创建Django项目(test) 进入配置文件settings.py 192.168.83.129:所需要远程连接数据库的ip地址 2.进入到远程连接的主机,修改/etc/mysql/mysql. ...
- JS笔记—01
1.JS的代码一般在头部写2.当页面载入时,会执行位于body部分的JavaScript当被调用时,位于head部分的JavaScript被执行.3.要对外部的JS文件的一个变量操作,代码是写在内部J ...