Codeforces 490E Restoring Increasing Sequence【二分+模拟】细节题
来源:互联网 发布:软件安全测试方法 编辑:程序博客网 时间:2024/05/16 07:06
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.
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.
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).
3?181?
YES11819
2???
NO
51222412??512226?0000?00000
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"); }}
- Codeforces 490E Restoring Increasing Sequence【二分+模拟】细节题
- Codeforces 490E. Restoring Increasing Sequence 二分
- CodeForces 490E Restoring Increasing Sequence
- CodeForces 490E Restoring Increasing Sequence
- Codeforces 490E Restoring Increasing Sequence(贪心)
- CodeForces 490E Restoring Increasing Sequence(贪心)
- CodeForces 490E Restoring Increasing Sequence
- Codeforces Round #279 (Div. 2) E. Restoring Increasing Sequence 二分
- codeforces 490E Restoring Increasing Sequence(贪心)
- Codeforces Round #279 (Div. 2) E. Restoring Increasing Sequence
- Codeforces Round #279 (Div. 2)E. Restoring Increasing Sequence
- Codeforces Round #279 (Div. 2) E. Restoring Increasing Sequence
- CodeForces - 11A Increasing Sequence
- codeforces 11A - Increasing Sequence
- CodeForces 11A - Increasing Sequence
- Codeforces 670E Correct Bracket Sequence Editor (list模拟)
- Codeforces 350 Div2 E Correct Bracket Sequence Editor(list模拟)
- Codeforces 670E Correct Bracket Sequence Editor【模拟双相链表】
- python中staticmethod classmethod及普通函数的区别
- 机房收费系统之九(导出Excel)
- ORACLE 多表连接与子查询
- MySQL行级锁、表级锁、页级锁详细介绍
- AndroidStudio ImageView全屏显示
- Codeforces 490E Restoring Increasing Sequence【二分+模拟】细节题
- 关于Android的PopupWindon
- 初学数据结构---查找代码之静态查找
- android 仿Boss直聘消息界面
- 浅析RPC机制
- 挑战实现P、R、F(精准率、召回率和调和数)的python程序
- 安装VMWare tools,以及解决安装后/mnt中有hgfs但没共享文件的方法
- win8.1 ubuntu14.04双系统 uefi引导问题
- .gitignore文件不起作用的解决办法 以及.DS_Store的处理