zoukankan      html  css  js  c++  java
  • [codeforces 509]C. Sums of Digits

    [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 sequence bi.

    Vasya wonders what the numbers ai could be like. Of all the possible options he likes the one sequence with the minimum possible last number an. Help Vasya restore the initial sequence.

    It is guaranteed that such a sequence always exists.

    输入

    The first line contains a single integer number n (1 ≤ n ≤ 300).

    Next n lines contain integer numbers b1, ..., bn  — the required sums of digits. All bi belong to the range 1 ≤ bi ≤ 300.

    输出

    Print n integer numbers, one per line — the correct option for numbers ai, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the i-th number should be equal to bi.

    If there are multiple sequences with least possible number an, print any of them. Print the numbers without leading zeroes.

    输入示例

    3
    3
    2
    1

    输出示例

    3
    11
    100

    数据规模及约定

    见“输入

    题解

    显然是贪心,尽量使当前数接近上一个数,且大于等于上一个数 + 1. 搞一个类似高精度运算的模拟。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <stack>
    #include <vector>
    #include <queue>
    #include <cstring>
    #include <string>
    #include <map>
    #include <set>
    using namespace std;
    
    const int BufferSize = 1 << 16;
    char buffer[BufferSize], *Head, *Tail;
    inline char Getchar() {
        if(Head == Tail) {
            int l = fread(buffer, 1, BufferSize, stdin);
            Tail = (Head = buffer) + l;
        }
        return *Head++;
    }
    int read() {
        int x = 0, f = 1; char c = getchar();
        while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
        while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
        return x * f;
    }
    
    #define maxn 310
    int n, B[maxn], num[maxn][maxn], len[maxn];
    
    int main() {
    	n = read();
    	for(int i = 1; i <= n; i++) B[i] = read();
    	
    	len[0] = 1;
    	for(int i = 1; i <= n; i++) {
    		num[i-1][1]++;
    		for(int j = 1; j <= len[i-1]; j++) {
    			num[i-1][j+1] += num[i-1][j] / 10;
    			num[i-1][j] %= 10;
    		}
    		for(int j = len[i-1] + 1; num[i-1][j]; j++) {
    			num[i-1][j+1] += num[i-1][j] / 10;
    			num[i-1][j] %= 10;
    			len[i-1] = j;
    		}
    		int tot = B[i], s = 0;
    		for(int j = 1; j <= len[i-1]; j++) s += num[i-1][j];
    		if(s == tot) memcpy(num[i], num[i-1], sizeof(num[i-1])), len[i] = len[i-1];
    		else {
    			len[i] = 0;
    			for(int j = len[i-1]; j; j--)
    				if(tot > num[i-1][j]) {
    					len[i] = max(len[i], j);
    					num[i][j] = num[i-1][j];
    					tot -= num[i][j];
    				}
    				else {
    					len[i] = max(len[i], j + 1);
    					num[i][j+1]++; tot--;
    					for(int k = j; k; k--) num[i][k] = 0;
    					break;
    				}
    			for(int j = 1; j <= len[i]; j++) {
    				num[i][j+1] += num[i][j] / 10;
    				num[i][j] %= 10;
    			}
    			for(int j = len[i] + 1; num[i][j]; j++) {
    				num[i][j+1] += num[i][j] / 10;
    				num[i][j] %= 10;
    				len[i] = j;
    			}
    			tot = B[i];
    			for(int j = 1; j <= len[i]; j++) tot -= num[i][j];
    //			for(int j = len[i]; j; j--) printf("%d", num[i][j]); putchar('
    ');
    			for(int j = 1; tot; j++) {
    //				printf("%d(%d) ", tot, num[i][j]);
    				if(tot >= 9 - num[i][j]) tot -= (9 - num[i][j]), num[i][j] = 9;
    				else num[i][j] += tot, tot = 0;
    				len[i] = max(len[i], j);
    			}
    //			putchar('
    ');
    		}
    		for(int j = len[i]; j; j--) printf("%d", num[i][j]); putchar('
    ');
    	}
    	
    	return 0;
    }
    /*
    5
    30
    29
    28
    42
    11
    */
    
  • 相关阅读:
    MongoDB副本集replica set(四)--成员配置
    MongoDB副本集replica set(三)--添加删除成员
    MongoDB副本集replica set (二)--副本集环境搭建
    MongoDB副本集replica set (一)--基础知识
    MongoDB文档(二)--查询
    MongoDB文档(一)--插入、更新、删除
    MongoDB主从复制(master-->slave)环境搭建
    终于,病毒向我伸出了魔爪......
    若感染病毒的是你,你又该如何?
    面试官:换人!他连哈希扣的都不懂
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5814794.html
Copyright © 2011-2022 走看看