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 of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.

Restore the the original sequence knowing digits remaining on the board.

Input

The first line of the input contains integer n (1 ≤ n ≤ 105) — the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.

Output

If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers — a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them.

If there is no answer, print a single line "NO" (without the quotes).

Sample test(s)
input
3
?
18
1?
output
YES
1
18
19
input
2
??
?
output
NO
input
5
12224
12??5
12226
?0000
?00000
output
YES
12224
12225
12226
20000
100000
题意:已知n个数按升序排列,但是某些数的某些位未知,构造一种方案,让n个数满足升序排列。

题解:从前往后构造,贪心让当前的数最小即可。代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<stdlib.h>
#include<vector>
#define inff 0x3fffffff
#define nn 110000
#define mod 1000000007
typedef long long LL;
const LL inf64=inff*(LL)inff;
using namespace std;
vector<string>ans;
string s,pre;
char sr[20];
int n;
bool r[10];
int main()
{
    int i,j;
    while(scanf("%d",&n)!=EOF)
    {
        ans.clear();
        pre="";
        int lp,ls;
        bool da;
        bool fuck=false;
        for(i=1;i<=n;i++)
        {
            scanf("%s",sr);
            if(fuck)
                continue;
            s=sr;
            lp=pre.size();
            ls=s.size();
            if(ls>lp)
            {
                for(j=0;j<ls;j++)
                {
                    if(s[j]=='?')
                    {
                        if(j==0)
                            s[j]='1';
                        else
                            s[j]='0';
                    }
                }
                ans.push_back(s);
                pre=s;
                continue;
            }
            if(ls<lp)
            {
                fuck=true;
                continue;
            }
            r[ls]=false;
            for(j=ls-1;j>=0;j--)
            {
                if(r[j+1])
                {
                    if(s[j]!='?')
                    {
                        if(s[j]>=pre[j])
                            r[j]=true;
                        else
                            r[j]=false;
                    }
                    else
                    {
                        r[j]=true;
                    }
                }
                else
                {
                    if(s[j]!='?')
                    {
                        if(s[j]>pre[j])
                            r[j]=true;
                        else
                            r[j]=false;
                    }
                    else
                    {
                        if(pre[j]=='9')
                            r[j]=false;
                        else
                            r[j]=true;
                    }
                }
            }
            if(!r[0])
            {
                fuck=true;
                continue;
            }
            da=false;
            for(j=0;j<ls;j++)
            {
                if(s[j]!='?'&&!da)
                {
                    if(s[j]>pre[j])
                        da=true;
                    continue;
                }
                if(s[j]=='?')
                {
                    if(da)
                        s[j]='0';
                    else
                    {
                        if(pre[j]=='9')
                            s[j]='9';
                        else
                        {
                            if(r[j+1])
                                s[j]=pre[j];
                            else
                            {
                                s[j]=pre[j]+1;
                                da=true;
                            }
                        }
                    }
                }
            }
            pre=s;
            ans.push_back(s);
        }
        if(fuck)
            puts("NO");
        else
        {
            puts("YES");
            for(i=0;i<n;i++)
            {
                for(j=0;j<(int)ans[i].size();j++)
                    printf("%c",ans[i][j]);
                puts("");
            }
        }
    }
    return 0;
}


### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值