Codeforces Round #279 (Div. 2) E 二分+技巧




链接:戳这里


E. Restoring Increasing Sequence
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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串数字,每一行表示一个正整数(最多八位),有些地方有'?'需要去填数。填完之后使得n行数严格递增


思路:

没有问号的一行直接过。

现在假设当前行有k个问号,那么我们需要填这k个问号,使得填出来的数尽可能的小而且要比前一项大。

k个问号相当于k个0~9的数字,填进去使组成的新的数满足要求。

k个数字也就是k位,这k位相当于k^10,也就是说填这k位可以组成k^10种新的数。

我们二分最小的那个满足条件的数,使得当前的新数填最接近的数满足大于前一项且最小。

这个二分很难想,很抽象,看了Q神代码,要不然模拟得伤脑筋。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
char s[20];
int last=0,anw[100100];
int ten[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
int calc(int x,int len){
    char tmp[20];
    int cnt=0;
    for(int i=0;i<len;i++){
        tmp[++cnt]=x%10+'0';
        x/=10;
    }
    int ans=0;
    for(int i=0;i<strlen(s);i++){
        if(s[i]!='?') ans=ans*10+s[i]-'0';
        else ans=ans*10+tmp[cnt--]-'0';
    }
    ///cout<<ans<<endl;
    return ans;
}
int check(){
    int len=0;
    for(int i=0;i<strlen(s);i++) {
        if(s[i]=='?') len++;
    }
    int l=0,r=ten[len],mid,ans=-1;
    if(s[0]=='?') l=ten[len-1];
   /// printf("%d %d %d\n",l,r,len);
    while(l<=r){
        mid=(l+r)/2;
        if(calc(mid,len)>last) {
            r=mid-1;
            ans=mid;
        }
        else l=mid+1;
    }
    if(ans==-1) return -1;
    return calc(ans,len);
}
int n;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",s);
        int tmp=check();
        if(tmp>0) anw[i]=last=tmp;
        else {
            cout<<"NO"<<endl;
            return 0;
        }
    }
    cout<<"YES"<<endl;
    for(int i=1;i<=n;i++) cout<<anw[i]<<endl;
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值