【题目链接】: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的更多相关文章

  1. 【81.37%】【codeforces 734B】Anton and Digits

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. [codeforces 509]C. Sums of Digits

    [codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...

  4. 【66.47%】【codeforces 556B】Case of Fake Numbers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【52.49%】【codeforces 556A】Case of the Zeros and Ones

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【42.59%】【codeforces 602A】Two Bases

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【63.73%】【codeforces 560A】Currency System in Geraldion

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【39.29%】【codeforces 552E】Vanya and Brackets

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【33.33%】【codeforces 552B】Vanya and Books

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. Android App调用MediaRecorder实现录音功能的实例【转】

    本文转载自:http://www.jb51.net/article/82281.htm 这篇文章主要介绍了Android App调用MediaRecorder实现录音功能的实例,MediaRecord ...

  2. Android指纹识别深入浅出分析到实战

    指纹识别这个名词听起来并不陌生,但是实际开发过程中用得并不多.Google从Android6.0(api23)开始才提供标准指纹识别支持,并对外提供指纹识别相关的接口.本文除了能适配6.0及以上系统, ...

  3. 让DIV在屏幕上下左右居中

    转自:http://blog.sina.com.cn/s/blog_65d41dff0100v0iz.html 其实解决的思路是这样的:首们需要position:absolute;绝对定位.而层的定位 ...

  4. B1297 [SCOI2009]迷路 矩阵

    这个题我觉得很有必要写一篇博客.首先,我们需要知道,假如一个邻接矩阵只有0/1构成,那么它自己的n次方就是走n步之后的方案数.但这个题还有2~9咋办呢.我们观察发现,这个题只有10个点,而且边权< ...

  5. ecshop的一些东西,来看看

    \includes\lib_commom.php =>公用函数库 \includes\lib_main.php =>前台公用函数库 \includes\lib_init.php => ...

  6. Ubuntu中安装部署Intel CS WebRTC

    1环境要求 组件 版本要求 OS CentOS* 7.4, Ubuntu 14.04/16.04 LTS Node 8.11.* (推荐8.11.1) MongoDB 2.4.9 Boost 1.65 ...

  7. 微信小程序之商品发布+编辑功能(多图片上传功能)

    小程序的商品发布页面:功能有多图片上传 遇到的问题记录一下:1.uploadFile成功之后返回的参数是json字符串,一定要用JSON.parse转换为object格式 2.因为商品发布和编辑都是在 ...

  8. 【转】MySQL存储引擎中的MyISAM和InnoDB区别详解

    转自:http://www.jb51.net/article/62457.htm MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Ac ...

  9. Struts2 之 实现文件上传(多文件)和下载

    Struts2  之 实现文件上传和下载 必须要引入的jar commons-fileupload-1.3.1.jar commons-io-2.2.jar 01.文件上传需要分别在struts.xm ...

  10. 设计模式——“signleton”

    那天别人问了我一个问题,关于单例模式的,由于之前了解的都是蜻蜓点水,所以重新复习了一次重新总结. 单例模式的写法总的来说有5种:懒汉,恶汉,枚举,双重校验锁,静态内部类 懒汉 public class ...