题意: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(贪心乱搞)题解的更多相关文章

  1. Codeforces 509C Sums of Digits 贪心

    这道题目有人用DFS.有人用DP 我觉得还是最简单的贪心解决也是不错的选择. Ok,不废话了,这道题目的意思就是 原先存在一个严格递增的Arrary_A,然后Array_A[i] 的每位之和为Arra ...

  2. Codeforces 509C Sums of Digits

    http://codeforces.com/contest/509/problem/C  题目大意: 给出一个序列,代表原序列对应位置数的每一位的数字之和,原序列单调递增,问原序列的最后一个数最小的方 ...

  3. 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 ...

  4. Codeforces 723c [贪心][乱搞]

    /* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给一个n和m. 第二行给n个数. 每次操作可以把n个数中的任何一个数替代为别的数,问最少的操作次数使得1.2.3.4.5...m中的数出现的次数 ...

  5. [CF752D]Santa Claus and a Palindrome(优先队列,贪心乱搞)

    题目链接:http://codeforces.com/contest/752/problem/D 题意:给长度为k的n个字符串,每一个字符串有权值,求构造一个大回文串.使得权值最大. 因为字符串长度都 ...

  6. Codeforces Gym 100203G Good elements 暴力乱搞

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以 ...

  7. [luoguP1053] 篝火晚会(贪心 + 乱搞)

    传送门 假设第一个位置是1,那么枚举它的左右两边是谁,有两种情况,然后可以递推求出序列. 然后可以贪心,两个序列有多少个不同的数,答案就是多少,具体为啥,yy一下即可 然后就是判断递推求出的序列和目标 ...

  8. FJNU2018低程F jq解救fuls (贪心乱搞)题解

    题目描述 一天fuls被邪恶的"咕咕咕"抓走了,jq为了救fuls可谓是赴汤蹈火,费了九牛二虎之力才找到了"咕咕咕"关押fuls的地方. fuls被关在一个机关 ...

  9. CodeForces 81D.Polycarp's Picture Gallery 乱搞

    D. Polycarp's Picture Gallery time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

随机推荐

  1. 域名重新绑定ip时访问地址NotFount404

    情形描述:部署在A服务器IIS上的asp.net程序,搬迁到B服务器上,重新绑定域名和ip后.再访问网址时有些电脑能正常访问,而有些电脑报404 not found错误. 经分析发现是个人电脑网络设置 ...

  2. Yii2 Restful api自定义资源

  3. [博客迁移]探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据

    上一篇博文介绍了如何连接Windows Azure: http://www.cnblogs.com/teld/p/5113063.html 本篇我们继续上次的示例代码,获取虚拟机的监控定义和监控数据. ...

  4. 【Hadoop学习之三】Hadoop全分布式安装

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop3.1.1 全分布式就是集群,注意配置主机名. ...

  5. SLAM学习笔记 - 世界坐标系到相机坐标系的变换

    参考自: http://blog.csdn.net/yangdashi888/article/details/51356385 http://blog.csdn.net/li_007/article/ ...

  6. PersistenceContext.properties()

    在做 Spring + SpringMVC + SpringData 时,单元测试 报这个错误: java.lang.NoSuchMethodError:javax.persistence.Persi ...

  7. python os.path.dirname()

    ----返回文件所在的路径 ----如果path变量直接是文件名则返回空

  8. python mmap对象

    ----使用内存映射的原因 为了随机访问文件的内容,使用mmap将文件映射到内存中是一个高效和优雅的方法.例如,无需打开一个文件并执行大量的seek(),read(),write()调用,只需要简单的 ...

  9. Maven依赖中的scope详解,在eclipse里面用maven install可以编程成功,到服务器上用命令执行报VM crash错误

    Maven依赖中的scope详解 项目中用了<scope>test</scope>在eclipse里面用maven install可以编译成功,到服务器上用命令执行报VM cr ...

  10. Linux 配置 JDK

    1. 上传 JDK 2. 解压文件 tar -xvf 文件名 3. 配置环境变量: 指令 vim /etc/profile 以上格式是不变的,使用时只改变 JAVA_HOME 和 JAVA_BIN 的 ...