Codeforces 509C Sums of Digits 贪心

这道题目有人用DFS、有人用DP

我觉得还是最简单的贪心解决也是不错的选择。

Ok,不废话了,这道题目的意思就是

原先存在一个严格递增的Arrary_A,然后Array_A[i] 的每位之和为Array_B[i]

现在给你一个Array_B, 让你在条件:

Array_A[len] Minimize

下求出次数组

(当然我们很容易得出,如果Array_A[len] 不是最小化的,那么答案有无穷多,随意暴力一下都可以)

所以这题没有那么暴力= =

解题思路:

首先求出Array_B[i] 和 Array_B[i - 1]的差 delta

如果delta > 0, 那么对Array_A从右到左依次加上去,易得在数据范围内不会TLE

使得最后的Array_A 为 (Carry)9*  // Carry为进位, 9*代表后面跟着n个0,n可为0

实现方法可以看下面的代码。

如果delta < 0,对Array_A 从右开始,低于delta的位数清零

同时把Array_A[i] 加进delta

因为接下来有一个操作,需要对Array_A[i]自加1

以防9 + 1 = 10这样的情形出现,所以需要将Array_A[i]开始连续为9的位都清零

同时把Array_A[i] 加进delta

然后重复delta > 0 的情形的操作

经测试最大的答案在300+位,所以数组可以适当开大一点...

Source code:

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <bits/stdc++.h>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int N = 1              ;
const int M = 200000         ;
const ll P = 10000000097ll   ;
const int INF = 0x3f3f3f3f   ;

int b1, b2;
int a[1000], len;

void output(){
    for(int i = len; i >= 1; --i){
        printf("%d",a[i]);
    }
    puts("");
}

void add(int num){
    int i = 1;
    while(num){
        if(9 == a[i]){
            ++i;
        }
        else{
            ++a[i];
            --num;
        }
    }
    checkmax(len, i);
}

void add_new(int num){
    int i = 1;
    while(num <= 0){
        num += a[i];
        a[i++] = 0;
    }
    while(9 == a[i]){
        num += a[i];
        a[i++] = 0;
    }
    ++a[i];
    --num;
    checkmax(len, i);
    add(num);
}

int main(){
    int i, j, t, n, m;
    while(EOF != scanf("%d",&t)){
        memset(a, 0, sizeof(a));
        len = 1;
        scanf("%d",&b1);
        add(b1);
        output();
        for(i = 1; i < t; ++i){
            scanf("%d",&b2);
            int delta = b2 - b1;
            b1 = b2;
            if(delta > 0)   add(delta);
            else    add_new(delta);
            output();
        }

    }

}
时间: 2025-01-05 03:50:41

Codeforces 509C Sums of Digits 贪心的相关文章

codeforces 509C Sums of Digits

codeforces 509C Sums of Digits 题意: 给出n个数字各位的加和bi,求一个严格递增的数列.要求最后一个数字最小. 如: 3 2 1 -> 3 11 100 限制: 1 <= n <= 300; 1 <= bi <=300 思路: 贪心,要求最后一个数字最小,只要保证一路过来的数字都尽量小就行. 令d=b[i]-b[i-1], 如果d>0,则从最低位填起,尽量把低位填到9 如果d<=0,则先从低位开始进位,使得d>0,然后就可以转

[codeforces 509]C. Sums of Digits

试题描述 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 sequen

Codeforces 223APartial Sums 数论+组合数学

题意很简单,求不是那么好求的,k很大 要操作很多次,所以不可能直接来的,印象中解决操作比较多无非线段树 循环节 矩阵 组合数等等吧,这道题目 也就只能多画画什么 的了 就以第一个案例为主吧 , 3 1 2 3 k我们依据画的次数来自己定好了 下面的每个数表示这个位置的 数由最初的 数组num[]中多少个数加起来得到的 当k为0的时候呢,就是 1 1 1 k为1的时候呢 1 2 3 k为2的时候呢 1 3 6 那么k为3的时候 1 4 10 这里看一下 从数组下标0开始,那么其实就是 C(i +

Codeforces 442B Andrey and Problem(贪心)

题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,现在他有n个朋友,每个朋友想出题目的概率为pi,但是他可以同时向多个人寻求帮助,不过他只能要一道题,也就是如果他向两个人寻求帮助,如果两个人都成功出题,也是不可以的. 解题思路:贪心,从概率最大的人开始考虑,如果询问他使得概率变大,则要询问. #include <cstdio> #include <cstring> #include <a

Codeforces Round #300-Tourist&#39;s Notes(贪心)

Tourist's Notes Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level

Codeforces 432E Square Tiling(构造+贪心)

我们通常这么写 using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; } 我的写数据 private void b

Codeforces 798D Mike and distribution - 贪心

Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, 

CF 489 C Given Length and Sum of Digits... 贪心

题目链接:http://codeforces.com/problemset/problem/489/C 题目大意:给定位数和各个位的和,问满足条件数字的最大值,最小值. 解题思路:模拟即可.主要是细节判断. 代码: 1 const int inf = 0x3f3f3f3f; 2 const int maxn = 1e2 + 5; 3 int m, s; 4 char ans1[maxn], ans2[maxn]; 5 6 void solve(){ 7 memset(ans1, 0, sizeo

codeforces 349B Color the Fence 贪心,思维

1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1-9每个字母分别要ai升油漆,问最多可画多大的数字. 贪心,也有点考思维. #include<bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f int main() { int v,a[1