Codeforces Round #279 (Div. 2) E. Restoring Increasing Sequence 二分

本文介绍了一种修复被部分数字替换为问号的严格递增整数序列的方法。通过特殊的二分查找策略,该算法能够有效地填充未知数字,确保序列保持严格递增且每个数字都是正整数。

摘要生成于 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个数,但是有些数字不知道,需要你填写,使得最后的序列为一个严格递增的序列
思路:一个二分,但是这个二分很特别,刚刚开始想,二分这个数,使得最小满足大于上一个数,
   也满足给你那个数的原来的那些数,但是发现不好写,转化一下,把原来给的数剔除掉,
   把那些问号拼凑成一个数,二分问号的这个数,然后分开填进去看是否满足;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
const ll INF=1e18+10,MOD=1e9+7;
char a[N][10],b[15];
int c[10];
int ans[N];
int check(int x,int pos)
{
    int len=strlen(a[pos]);
    for(int i=0;i<len;i++)
        b[i]=a[pos][i];
    for(int i=len-1;i>=0;i--)
    {
        if(b[i]=='?')
        {
            b[i]=x%10+'0';
            x/=10;
        }
    }
    int ans=0;
    for(int i=0;i<len;i++)
        ans=ans*10+b[i]-'0';
    return ans;
}
int lower(int x,int pos)
{
    int flag=0;
    int st=0;
    int en=1;
    int ans=-1;
    for(int i=0;i<strlen(a[pos]);i++)
    {
        if(a[pos][i]=='?')
            flag++,en*=10;;
    }
    if(a[pos][0]=='?')
        st=en/10;
    en--;
    while(st<=en)
    {
        int mid=(st+en)>>1;
        //cout<<mid<<" "<<check(x,pos)<<" "<<x<<" "<<endl;
        if(check(mid,pos)>x)
        {
            ans=mid;
            en=mid-1;
        }
        else
            st=mid+1;
    }
    if(ans==-1)
        return -1;
    for(int i=strlen(a[pos])-1;i>=0;i--)
    {
        if(a[pos][i]=='?')
        {
            a[pos][i]=ans%10+'0';
            ans/=10;
        }
    }
    int sum=0;
    for(int i=0;i<strlen(a[pos]);i++)
        sum=sum*10+a[pos][i]-'0';
    return sum;
}
int main()
{

    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%s",&a[i]);
    int pre=0;
    for(int i=1;i<=n;i++)
    {
        ans[i]=lower(pre,i);
        if(ans[i]==-1)
            return puts("NO\n");
        pre=ans[i];
    }
    printf("YES\n");
    for(int i=1;i<=n;i++)
    {
        printf("%d\n",ans[i]);
    }
    return 0;
}

 


转载于:https://www.cnblogs.com/jhz033/p/6034900.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值