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

来源:互联网 发布:软件安全测试方法 编辑:程序博客网 时间:2024/05/16 07:06

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. Nextn lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit0. Each element has length from 1 to 8 characters, inclusive.

Output

If the answer exists, print in the first line "YES" (without the quotes). Nextn 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?181?
Output
YES11819
Input
2???
Output
NO
Input
51222412??512226?0000?00000
Output
YES12224122251222620000100000

题目大意:

让你在?处填入合适的数字,使得长度为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");    }}






阅读全文
0 0
原创粉丝点击