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 ...
随机推荐
- netcore webapi统一配置跨域问题
修改startup类中的configure方法
- would you please...could you please...两句区别是什么?
Could you please 是can you please 更为礼貌.委婉的说法,并不是过去式,是“能否麻烦你……”.“请你……”的意思,更侧重“能否”及客观情况:回答时要注意,eg:A:“Co ...
- Oracle10g 连接 sqlserver hsodbc dblink 方式 非透明网关
Oracle10g 连接 sqlserver hsodbc dblink 方式 非透明网关 那个上传图片太麻烦了,发布到百度文库了 http://wenku.baidu.com/view/b38ae8 ...
- html5-表单和input元素用法
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 如何在Sitecore CMS中管理桌面快捷方式
当您在Sitecore的桌面模式下工作时,创建快捷方式很有用.快捷方式允许您在选择特定项目的情况下打开内容编辑器,而无需深入了解内容树. Sitecore 8 Sitecore 7 Sitecore ...
- HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor
看到的最长的类名: HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhere ...
- C#使用SmtpClient发送邮件
目的:写一个可发送邮件的DLL. 原理: 例如A使用163邮箱发送邮件给B(qq邮箱).首先A会把邮件通过SMTP(Simple Mail Transfer Protocol)协议传输到163的Smt ...
- 记账本微信小程序开发一
第一,在微信公众平台注册小程序账号并完善相关信息 第二,注册一个微信公众号,找到微信web开发工具并下载适合自己电脑的工具 第三,安装 第四,根据网上教程简单了解了开发工具的使用和布局
- VMWare常用快捷键
VMWare常用快捷键 Ctrl-Alt-Enter 进入全屏模式 ctrl+alt+insert 退出全屏 Ctrl-Alt 返回正常(窗口)模式 Ctrl-A ...
- Java开发软件安装及配置
2017-6-1 1.jdk 1.8.0_131 安装JDK 选择安装目录 安装过程中会出现两次 安装提示 . 第一次是安装 jdk ,第二次是安装 jre .建议两个都安装在同一个java文件夹中的 ...