CodeForces 490E Restoring Increasing Sequence(贪心)

CodeForces 490E

题目大意:给N个正整数,然而这些正整数中间有些数字是被‘?’遮挡住了,每个‘?’可以还原回一个数字,希望给定的这N个整数形成一个递增的序列。可以的话,给出这N个整数的序列,不行返回N0.

解题思路:每个整数都在满足条件的情况下尽量的小,写了一个非递归版本的,发现要考虑的因素太多了,wa了太多遍。后面改成了递归版本的,但是这个比较耗时。

代码:

#include <cstdio>
#include <cstring>

const int MAXL = 10;
const int MAXN = 1e5 + 5;
char str[MAXN][MAXL];
int len[MAXN];

int N;

bool dfs (int i, int l1, int flag) {            

    if (l1 >= len[i]) {
        if (flag)
            return true;
        return false;
    }

    if (str[i][l1] != ‘?‘) {

        if (!flag && str[i][l1] < str[i - 1][l1])
            return false;
        return dfs(i, l1 + 1, flag | str[i][l1] > str[i - 1][l1]);
    } else {

        if (flag) {
            str[i][l1] = ‘0‘;
            if (dfs(i, l1 + 1, flag))
                return true;
            str[i][l1] = ‘?‘;
            return false;
        }

        for (char k = str[i - 1][l1]; k <= ‘9‘; k++) {
            str[i][l1] = k;
            if (dfs(i, l1 + 1, k > str[i - 1][l1]))
                return true;
        }
        str[i][l1] = ‘?‘;
        return false;
    }
}

bool judge(int i) {

    if (len[i] < len[i - 1])
        return false;

    if (len[i] > len[i - 1]) {
        if (str[i][0] == ‘?‘)
            str[i][0] = ‘1‘;
        for (int j = 1; j < len[i]; j++)
            if (str[i][j] == ‘?‘)
                str[i][j] = ‘0‘;
        return true;
    }

    if (dfs(i, 0, 0))
        return true;
    return false;
}

bool handle () {
    if (N == 1)
        return true;
    for (int i = 1; i < N; i++)
        if (!judge(i))
            return false;
    return true;
}

void handle_first() {
    int len = strlen(str[0]);
    for (int i = 0; i < len; i++)
        if (str[0][i] == ‘?‘)
            str[0][i] = (i == 0) ?‘1‘ : ‘0‘;
}

int main () {

    while (scanf ("%d", &N) != EOF) {

        for (int i = 0; i < N; i++) {
            scanf ("%s", str[i]);
            len[i] = strlen(str[i]);
        }

        handle_first();
        if (handle()) {
            printf ("YES\n");
            for (int i = 0; i < N; i++)
                printf ("%s\n", str[i]);
        } else
            printf ("NO\n");
    }
    return 0;
}
时间: 2024-11-08 16:42:54

CodeForces 490E Restoring Increasing Sequence(贪心)的相关文章

Codeforces 490E. Restoring Increasing Sequence 二分

统计有几个'?',然后二分检测取满足条件的最小的数就可以了. 注意没有问号的时候也要检测一下.... E. Restoring Increasing Sequence time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Peter wrote on the board a strictly increasing sequence o

CodeForces 490E Restoring Increasing Sequence

题意: 一个严格递增序列  某些数字的某些位被盖住了  求  恢复后的序列 思路: 贪心  让每个数在大于前一个的基础上尽量的小 先讨论数字长度 len[i]<len[i-1] 一定是NO len[i]>len[i-1] 除了第一位如果是?就填1以外  其他?全填0 len[i]==len[i-1] dfs搜索num[i]格式下大于num[i-1]的最小的数 代码: #include<cstdio> #include<iostream> #include<cstr

Increasing Sequence CodeForces - 11A

Increasing Sequence CodeForces - 11A 很简单的贪心.由于不能减少元素,只能增加,过程只能是从左到右一个个看过去,看到一个小于等于左边的数的数就把它加到比左边大,并记录加的次数. 错误记录: 但是很容易错...以前错了4次..过几个月来再做还是不能1A... 比如下面这个有很明显错误的程序 1 #include<cstdio> 2 int n,d,last,now,ans; 3 int main() 4 { 5 int i; 6 scanf("%d%

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 250B Restoring IPv6 解题报告

Description An IPv6-address is a 128-bit number. For convenience, this number is recorded in blocks of 16 bits in hexadecimal record, the blocks are separated by colons — 8 blocks in total, each block has four hexadecimal digits. Here is an example o

HDU5014Number Sequence(贪心)

HDU5014Number Sequence(贪心) 题目链接 题目大意: 给出n,然后给出一个数字串,长度为n + 1, 范围在[0, n - 1].然后要求你找出另外一个序列B,满足上述的要求,而且使得t = A0^B0 + Ai + 1 ^ Bi + 1 + ... + An ^ Bn 最大. 解题思路: 对于一个数字进行异或,要求结果最大的话,那么取这个数字的二进制互补数字是最好的情况,而且能够发现每次找到一个数字和相应的互补的数字都会是一段区间.就这样一段一段区间的去寻找每一个点相应的

Codeforces 442B Andrey and Problem(贪心)

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

HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi&#39;an Online

尽可能凑2^x-1 #include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0;

hdu4915 Parenthese sequence 贪心O(n)解法

hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 82    Accepted Submission(s): 25 Problem Description bobo found an ancient string. The string contains only three c