跳到正文

题解

GWOI Beginning Contest J3 / #1 -zh

Mason123456约 13 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月30日
篇幅
约 13 分钟

版本

  1. v1当前

GBC J3 / #1 题解

至于为什么 Round #1 不是 J1,因为 J1,J2 都已经有题了,J3 是第一套出好的题。

为了方便对拍,我甚至把官方的 Gen 放出来,小数据都打好了。

J3-T1

CF1861B

思路

一个有意思的条件,以 00 开头,11 结尾。如果我们可以把字符串处理成 0…01…10 \dots 01 \dots 1 的形式,那两个字符串就相等了。

于是我们需要找到 ii 使得 [ai,ai+1]=[bi,bi+1]=[0,1][a_i, a_{i+1}] = [b_i, b_{i + 1}] = [0,1],直接枚举即可。

Code

C++
#include <iostream>
#include <cstring>
using namespace std;

void sol(){
    string a, b;
    cin>>a>>b;
    for(int i = 0;i < (int)a.size() - 1;i++){
        if(a[i] == b[i] && a[i + 1] == b[i + 1] && a[i] == '0' && a[i + 1] == '1'){
            cout<<"YES\n";
            return;
        }
    }
    cout<<"NO\n";
}

int main() {
    int n;
    cin>>n;
    while(n--){
        sol();
    }
    return 0;
}

Python

Python
def sol():  
    a = input().strip()  
    b = input().strip()  
    for i in range(len(a) - 1):  
        if a[i] == b[i] and a[i + 1] == b[i + 1] and a[i] == '0' and a[i + 1] == '1':  
            print("YES")  
            return  
    print("NO")  

def main():  
    n = int(input().strip())  
    for _ in range(n):  
        sol()  

if __name__ == "__main__":  
    main()

Gen

C++
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
    srand(time(nullptr));
//    int n = (int)10 - (rand() % 3);
    int n = (int)2e3 - (rand() % 3);
    cout<<n<<"\n";
    while(n--){
//        int x = (int)10 - 2 - (rand() % 5);
        int x = (int)5e3 - 2 - (rand() % 5);
        cout<<0;
        for(int i = 1;i <= x;i++)   cout<<rand() % 2;
        cout<<"1\n";
        cout<<0;
        for(int i = 1;i <= x;i++)   cout<<rand() % 2;
        cout<<"1\n";
    }
    return 0;
}

J3-T2

CF1913C

思路

这题其实并不难。可以发现 x≤29x \le 29,是一个非常小的范围。我们直接用 aia_i 表示 2i2^i 有多少个。每次询问 ww 的时候,从右往左枚举 ww 在二进制下的最后一位,并将 aia_i 备份一份到 bib_i 并且进位。如果枚举到第 ii 位 =1=1 且 bi=0b_i = 0,说明不能凑出 ww。

Code

C++
#include <iostream>
using namespace std;
const int N = 35;
int a[N], b[N], n;
int t, x;

void sol(){
    for(int i = 0;i <= 30;i++)  b[i] = a[i];
    for(int i = 0;i <= 30;i++){
        if(x & (1 << i)){
            if(b[i])    b[i]--;
            else{
                cout<<"NO\n";
                return;
            }
        }
        b[i + 1] += b[i] / 2;
    }

    cout<<"YES\n";
}

int main() {
    cin>>n;
    while(n--){
        cin>>t>>x;
        if(t == 1){
            a[x]++;
        }
        else{
            sol();
        }
    }
    return 0;
}

Python

Python
def sol():  
    b = a[:]  
    for i in range(31):  
        if x & (1 << i):  
            if b[i]:  
                b[i] -= 1  
            else:  
                print("NO")  
                return  
        b[i + 1] += b[i] // 2  
    print("YES")  

def main():  
    n = int(input().strip())  
    for _ in range(n):  
        global x  
        t, x = map(int, input().strip().split())  
        if t == 1:  
            a[x] += 1  
        else:  
            sol()  

if __name__ == "__main__":  
    N = 35  
    a = [0] * N  
    main()

Gen

C++
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
    srand(time(0));
//    int n = (int)10 - (rand() % 5);
    int n = (int)1e5 - (rand() % 5);
    cout<<n<<"\n";
    while(n--){
        int op = rand() % 2 + 1;
        cout<<op<<" ";
        if(op == 1){
//            cout<<rand() % 5 + 1<<"\n";
            cout<<rand() % 29 + 1<<"\n";
        }
        else{
//            cout<<rand() % ((int)40 + 1)<<"\n";
            cout<<rand() % ((int)1e9 + 1)<<"\n";
        }
    }
    return 0;
}

J3-T3

CF1849C

思路

假设给定 [l,r][l, r],考虑如果 [l1,r1][l_1, r_1] 的结果与 [l2,r2][l_2, r_2] 的结果一样,那么说明 [l1,r1],[l2,r2][l_1, r_1],[l_2, r_2] 所真正排序的区间是一样的。

那它们真正排序的区间是「l1l_1 以后的第一个 11」到「r1r_1 以前的第一个 00」

最后求出真正排序的区间,用 map 去重即可。

Code

C++
// c++14
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
const int N = 2e5 + 5;

map<pair<int, int>, int> vis;
int n, m;
int nxt[N], pre[N];
char str[N];

void sol(){
    vis.clear();
    int ans = 0;
    cin>>n>>m;
    cin>>(str + 1);
    nxt[n + 1] = n + 1;
    for(int i = 1;i <= n;i++){
        if(str[i] == '1')   pre[i] = pre[i-1];
        else    pre[i] = i;
    }
    for(int i = n; i; i--){
        if(str[i] == '1')   nxt[i] = i;
        else    nxt[i] = nxt[i + 1];
    }
    while(m--){
        int l, r;
        cin>>l>>r;
        l = nxt[l], r = pre[r];
        if(l > r)   l = r = 0;
        if(!vis[{l, r}])    ans++, vis[{l, r}] = 1;
    }
    cout<<ans<<"\n";
}

int main() {
    int T;
    cin>>T;
    while(T--){
        sol();
    }
    return 0;
}

Python

Python
def sol():  
    vis.clear()  
    ans = 0  
    n, m = map(int, input().strip().split())  
    str_input = input().strip()  
    str_input = ' ' + str_input  # This mimics the C++ 1-based index adjustment  
    nxt[n + 1] = n + 1  
    for i in range(1, n + 1):  
        if str_input[i] == '1':  
            pre[i] = pre[i - 1]  
        else:  
            pre[i] = i  
    for i in range(n, 0, -1):  
        if str_input[i] == '1':  
            nxt[i] = i  
        else:  
            nxt[i] = nxt[i + 1]  
    for _ in range(m):  
        l, r = map(int, input().strip().split())  
        l = nxt[l]  
        r = pre[r]  
        if l > r:  
            l = r = 0  
        if (l, r) not in vis:  
            ans += 1  
            vis[(l, r)] = 1  
    print(ans)  

def main():  
    T = int(input().strip())  
    for _ in range(T):  
        sol()  

if __name__ == "__main__":  
    N = int(2e5 + 5)  
    nxt = [0] * (N + 1)  
    pre = [0] * (N + 1)  
    vis = {}  
    main()

Gen

C++
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
    srand(time(nullptr));
//    int t = 5;
    int t = (int)1e4 - (rand() % 5);
    cout<<t<<"\n";
    for(int _ = 1;_ <= t;_++){
//        int n = 10;
        int n = int(2e5) / t;
        int m = n;
        cout<<n<<" "<<m<<"\n";
        for(int i = 1;i <= n;i++){
            cout<<rand() % 2;
        }
        cout<<"\n";
        while(m--){
            int l = rand() % n + 1;
            int r = l + rand() % (n - l + 1);
            cout<<l<<" "<<r<<"\n";
        }
    }
    return 0;
}
原文