Codeforces Round #279 (Div. 2) E. 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).

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
");
        pre=ans[i];
    }
    printf("YES
");
    for(int i=1;i<=n;i++)
    {
        printf("%d
",ans[i]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/jhz033/p/6034900.html