Codeforces 490E Restoring Increasing Sequence【二分+模拟】细节题

本文介绍了一种算法,用于解决在给定包含问号的数列中填充合适的数字,以确保数列严格递增且无前导零的问题。通过贪心策略或二分查找方法实现,详细解释了算法步骤及AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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).

Examples
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的数字序列是严格递增的,不允许有前导0。


思路:


我们可以选择直接贪心做法,每次比较相邻两个数字串,然后贪心的将下边的数字串改为大于上边的数字串的最小的那个即可。

细节很多。慢慢写肯定可以过。


又因为数字串很短,我们可以取出所有?,然后设定其l=0.r=9999.....(?的个数个9);

然后我们二分答案,check是否存在前导0以及是否可行,如果可行,减小答案,否则增大答案,我们这里是确实存在单调性的。

细节很多,但是相对而言,比贪心的细节要少很多的。


Ac代码:


#include<stdio.h>
#include<string.h>
using namespace std;
char a[100500][10];
void init()
{
    int tm=0;
    for(int i=0;i<strlen(a[0]);i++)
    {
        if(a[0][i]!='0'&&a[0][i]!='?')tm=1;
        if(a[0][i]=='?')
        {
            if(tm==0)
            {
                tm=1,a[0][i]='1';
            }
            else a[0][i]='0';
        }
    }
}
int Slove(int pos,int mid,int len)
{
    int cnt=0;
    int dig[10];
    while(cnt<len)
    {
        dig[cnt++]=mid%10;
        mid/=10;
    }
    cnt--;
    int sum=0;
    int flag=0;
    for(int i=0;i<strlen(a[pos]);i++)
    {
        if(a[pos][i]=='?')
        {
            if(flag==0&&dig[cnt]==0)return 0;
            sum=sum*10+dig[cnt];
            flag=1;
            cnt--;
        }
        else
        {
            flag=1;
            sum=sum*10+a[pos][i]-'0';
        }
    }
    int sumpre=0;
    for(int i=0;i<strlen(a[pos-1]);i++)
    {
        sumpre=sumpre*10+a[pos-1][i]-'0';
    }
    if(sum>sumpre)return 1;
    else return 0;
}
void Add(int pos,int mid,int len)
{
    int cnt=0;
    int dig[10];
    while(cnt<len)
    {
        dig[cnt++]=mid%10;
        mid/=10;
    }
    cnt--;
    for(int i=0;i<strlen(a[pos]);i++)
    {
        if(a[pos][i]=='?')a[pos][i]=dig[cnt--]+'0';
    }
}
int main()
{
    int n,ans,l,r;
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)scanf("%s",a[i]);
        int flag=0;
        init();
        for(int i=1;i<n;i++)
        {
            int len=strlen(a[i]);
            int cnt=0;
            ans=-1,l=0,r=0;
            for(int j=0;j<len;j++)
            {
                if(a[i][j]=='?')
                {
                    cnt++;
                    if(r==0)r=9;
                    else r=r*10+9;
                }
            }
            while(r-l>=0)
            {
                int mid=(l+r)/2;
                if(Slove(i,mid,cnt)==1)
                {
                    ans=mid;
                    r=mid-1;
                }
                else l=mid+1;
            }
            if(ans==-1)
            {
                flag=1;
                break;
            }
            else Add(i,ans,cnt);
        }
        if(flag==0)
        {
            printf("YES\n");
            for(int i=0; i<n; i++)printf("%s\n",a[i]);
        }
        else printf("NO\n");
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值