Codeforces 490E. Restoring Increasing Sequence 二分
2015-03-14 12:56
176 查看
统计有几个'?',然后二分检测取满足条件的最小的数就可以了. 注意没有问号的时候也要检测一下....
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).
Sample test(s)
input
output
input
output
input
output
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).
Sample test(s)
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
/* *********************************************** Author :CKboss Created Time :2015年03月14日 星期六 00时09分50秒 File Name :CF490E_2.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; char str[11]; int nine[10]={0,9,99,999,9999,99999,999999,9999999,99999999,999999999}; int len,ni; vector<int> vi; bool flag=true; int fenjie[11],fn; int getNUMBER(int mid) { int tmid=mid; fn=0; if(mid==0) fenjie[fn++]=0; while(mid) { fenjie[fn++]=mid%10; mid/=10; } for(int i=fn;i<ni;i++) fenjie[fn++]=0; reverse(fenjie,fenjie+fn); int ret=0,nx=0; for(int i=0;i<len;i++) { int cs=0; if(str[i]!='?') cs=str[i]-'0'; else if(nx<fn) cs=fenjie[nx++]; ret=ret*10+cs; } return ret; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T_T; vi.push_back(0); scanf("%d",&T_T); while(T_T--) { scanf("%s",str); len=strlen(str); if(flag==false) continue; /// find ? ni=0; for(int i=0;i<len;i++) if(str[i]=='?') ni++; bool FUCK = false; if(str[0]=='?') FUCK=true; if(ni==0) { int nb=0; for(int i=0;i<len;i++) nb=nb*10+(str[i]-'0'); if(nb>*(--vi.end())) vi.push_back(nb); else flag=false; } else { int low=0,high=nine[ni],ans=-1; int lastnumber=*(--vi.end()); if(FUCK) low=(int)(pow(10,ni-1)); while(low<=high) { int mid=(low+high)/2; int num=getNUMBER(mid); if(num>lastnumber) { ans=num; high=mid-1; } else low=mid+1; } if(ans==-1) flag=false; else vi.push_back(ans); } } if(flag) { puts("YES"); for(int i=1,sz=vi.size();i<sz;i++) printf("%d\n",vi[i]); } else puts("NO"); return 0; }
相关文章推荐
- codeforces 812C——Sagheer and Nubian Market(二分)
- CodeForces 363D Renting Bikes (二分+贪心)
- Codeforces 689D Friends and Subsequences(二分+RMQ)
- Codeforces 344E Read Time [二分答案]【思维】
- codeforces 817C Really Big Numbers 二分
- 【打CF,学算法——三星级】CodeForces 567D One-Dimensional Battle Ships (二分)
- Codeforces 75C (二分+数学)
- Codeforces 361D Levko and Array(二分)(DP)
- CodeForces - 672D _Robin Hood_二分
- CodeForces - 165B Burning Midnight Oil (二分)
- codeforces 650B . Image Preview 二分
- 【codeforces 782B】The Meeting Place Cannot Be Changed(二分)
- codeforces 672D D. Robin Hood (二分+思维)
- Codeforces-689 D Friends and Subsequences(RMQ+二分)
- Codeforces 497B Tennis Game( 枚举+ 二分)
- codeforces 475D CGCDSSQ ST表+二分
- CodeForces 780B The Meeting Place Cannot Be Changed【二分】
- CodeForces - 237C Primes on Interval 【二分】
- codeforces 655C C. Enduring Exodus(二分)
- CodeForces 672D (思维+二分)