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+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+,MOD=1e9+;
char a[N][],b[];
int c[];
int ans[N];
int check(int x,int pos)
{
int len=strlen(a[pos]);
for(int i=;i<len;i++)
b[i]=a[pos][i];
for(int i=len-;i>=;i--)
{
if(b[i]=='?')
{
b[i]=x%+'';
x/=;
}
}
int ans=;
for(int i=;i<len;i++)
ans=ans*+b[i]-'';
return ans;
}
int lower(int x,int pos)
{
int flag=;
int st=;
int en=;
int ans=-;
for(int i=;i<strlen(a[pos]);i++)
{
if(a[pos][i]=='?')
flag++,en*=;;
}
if(a[pos][]=='?')
st=en/;
en--;
while(st<=en)
{
int mid=(st+en)>>;
//cout<<mid<<" "<<check(x,pos)<<" "<<x<<" "<<endl;
if(check(mid,pos)>x)
{
ans=mid;
en=mid-;
}
else
st=mid+;
}
if(ans==-)
return -;
for(int i=strlen(a[pos])-;i>=;i--)
{
if(a[pos][i]=='?')
{
a[pos][i]=ans%+'';
ans/=;
}
}
int sum=;
for(int i=;i<strlen(a[pos]);i++)
sum=sum*+a[pos][i]-'';
return sum;
}
int main()
{ int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%s",&a[i]);
int pre=;
for(int i=;i<=n;i++)
{
ans[i]=lower(pre,i);
if(ans[i]==-)
return puts("NO\n");
pre=ans[i];
}
printf("YES\n");
for(int i=;i<=n;i++)
{
printf("%d\n",ans[i]);
}
return ;
}

上一篇:Codeforces Round #460 (Div. 2) ABCDE题解


下一篇:Codeforces Round #279 (Div. 2) C. Hacking Cypher 前缀+后缀