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;
}



Codeforces Round 1036 是一场同时面向 Div.1 和 Div.2 参赛者的比赛,通常这类比赛会包含多个具有挑战性的编程题目,涵盖算法、数据结构、数学等多个领域。比赛的题解和题目信息可以帮助参赛者回顾解题思路,提升编程能力。 ### 比赛基本信息 - **比赛名称**:Codeforces Round #1036 (Div. 1 and Div. 2) - **比赛时间**:具体时间为 UTC+X(根据实际举办日期和时间表) - **比赛链接**:[Codeforces 官方页面](https://codeforces.com/contest/1343) - **题解发布位置**:通常在比赛结束后不久,官方或社区成员会在 Codeforces 博客、GitHub 或其他技术平台上发布题解。 ### 题目类型与难度分布 该轮比赛通常包括 5 到 7 道题目,难度从简单实现到复杂算法不等。例如: - **A题**:通常是简单的模拟或数学问题。 - **B题**:可能涉及字符串处理或基础贪心策略。 - **C题**:中等难度,可能需要掌握基本的数据结构如数组、排序等。 - **D题及以后**:较高难度,可能涉及图论、动态规划、数论等高级算法。 ### 参赛情况与亮点 - **参与人数**:通常超过 10,000 名选手参加。 - **热门话题**:比赛中某些题目可能会引发广泛讨论,尤其是那些需要用到巧妙构造或优化技巧的问题。 - **知名选手表现**:顶尖选手如 tourist、Um_nik 等通常会以极快的速度完成所有题目,并占据排行榜前列。 ### 示例代码片段 以下是一个典型的 Codeforces 题目解法示例,适用于某道中等难度题目: ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while(t--) { long long l, r; cin >> l >> r; // 假设 e 是一个预处理好的符合条件的数组 // 使用二分查找来统计区间 [l, r] 内的有效数字个数 long long ans = upper_bound(e.begin(), e.end(), r) - lower_bound(e.begin(), e.end(), l); cout << ans << endl; } return 0; } ``` ### 题解资源推荐 - **Codeforces 官方博客**:通常会有详细的题解和作者说明。 - **GitHub 仓库**:许多参赛者会将自己的解法上传至 GitHub,便于他人学习。 - **知乎专栏 / 优快云 / 博客园**:中文社区中也常有高质量的赛后总结与分析文章。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值