3 分钟阅读

CodeForces1017 div4 题解

A.

输入三个字符串,输出每个字符串的首字符


B.

已知最终的区间 \([l, r]\) ,这是n天之后的结果,求解从开始到第m天的结果

注意 \(l \leq 0 \leq r\) ,可以进行逆向推导,也就是通过最终结果往前推导 n - m 天

但是需要注意不要越界,\(l = 0 \mid r = 0\) 时,则停止当前边的遍历,转向另一边

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int t; cin >> t;
    while (t--) {
        int n, m, l, r; cin >> n >> m >> l >> r;
        int cost = n - m;
        
        while (cost) {
            if (l == 0) break;
            l++;
            cost--;
        }
        
        if (cost) {
            r -= cost;
        }
        
        cout << l << ' ' << r << '\n';
    }
    
    return 0;
}

C.

得到一个 n * n 的二维数组,保证1个长度为2*n的排列一定存在

目前可以知道这个排列就是第一列+最后一行或者第一行+最后一列(去除重复元素)

缺少的那个元素是排列中存在,但遍历不存在。

解法:先遍历第一行和最后一列,遍历到的数字push_back()的一个output数组后面

遍历完成后,找出那个缺少的元素,优先输出这个缺少的元素,然后逐个输出output中的元素

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        vector<vector<int>> g(n, vector<int>(n));
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cin >> g[i][j];
            }
        }
        
        vector<int> output;
        vector<int> a(2000, 0);
        
        // 遍历第一行
        for (int i = 0; i < n; i++) {
            output.push_back(g[0][i]);
            a[g[0][i]]++;
        }
        
        // 遍历最后一列
        for (int i = 1; i < n; i++) {
            output.push_back(g[i][n-1]);
            a[g[i][n-1]]++;
        }
        
        for (int i = 1; i <= 2 * n; i++) {
            if (a[i] == 0) {
                cout << i << ' ';
                break;
            }
        }
        
        for (int i = 0; i < output.size(); i++) {
            cout << output[i];
            if (i != output.size() - 1) {
                cout << ' ';
            } else {
                cout << '\n';
            }
        }
    }
    
    return 0;
}

D.

题目给了两个字符串p, s,判断s是否通过p来进行生成的?

\(p_i = L\) ,可以在s生成1个”L”或者2个“L”

\(p_i=R\),可以生成1、2个“R”

目前的思路是,计算p从左往右,最开始相同字符的数量,再计算s相同字符的数量

满足一个公式:\(S_{cnt} = (P_{cnt}, ....,2*P_{cnt})\)

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int t; cin >> t;
    while (t--) {
        string p, s; cin >> p >> s;
        int p_cnt = 1, s_cnt = 0;
        int j = 0;
        char cur_ch = p[0]; 
        bool flag = true;
        
        for (int i = 1; i < p.size(); i++) {
            if (p[i] != cur_ch) {
                while (j < s.size() && s[j] == cur_ch) {
                    s_cnt++;
                    j++;
                }
                
                if (s_cnt < p_cnt || s_cnt > 2 * p_cnt) {
                    flag = false;
                    break;
                }
                
                cur_ch = p[i];
                p_cnt = 1;
                s_cnt = 0;
            } else {
                p_cnt++;
            }
        }
        
        if (flag) {
            while (j < s.size() && s[j] == cur_ch) {
                s_cnt++;
                j++;
            }
            
            if (s_cnt < p_cnt || s_cnt > 2 * p_cnt) {
                flag = false;
            }
        }
        
        if (j != s.size()) {
            flag = false;
        }
        
        cout << (flag ? "YES" : "NO") << '\n';
    }
}

GPT的思路:

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        string p, s;
        cin >> p >> s;

        int i = 0, j = 0;
        bool ok = true;

        while (i < (int)p.size() && j < (int)s.size()) {
            // 当前连续段的字符必须相同
            if (p[i] != s[j]) {
                ok = false;
                break;
            }

            char ch = p[i];
            int cntP = 0, cntS = 0;

            // 统计 p 中当前字符的连续段长度
            while (i < (int)p.size() && p[i] == ch) {
                cntP++;
                i++;
            }

            // 统计 s 中当前字符的连续段长度
            while (j < (int)s.size() && s[j] == ch) {
                cntS++;
                j++;
            }

            if (cntS < cntP || cntS > 2 * cntP) {
                ok = false;
                break;
            }
        }

        // 必须保证两个字符串都处理完了
        if (i != (int)p.size() || j != (int)s.size()) {
            ok = false;
        }

        cout << (ok ? "YES" : "NO") << '\n';
    }

    return 0;
}

E.

给定一个数组a,我需要在数组中找到一个数,使得这个数与数组中每一个数异或求和的值最大

\[F(x)=\sum_{i=1}^{n}(x\oplus a_i)\]

统计每一个数在对应数位上的值,可以用二进制来统计,将出现1和出现0的次数记录

然后计算每一个数对应的权值

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

void cal(int n, vector<int>& cnt) {
	for (int i = 0; i < 30; i++) {
        if (n == 0) break;
        cnt[i] += n % 2;
        n /= 2;
    }
}

i64 calQ(vector<int>& cnt, i64 n, int total) {
    i64 base = 1;
    i64 q = 0;
    for (int i = 0; i < 30; i++) {
        if (n % 2 == 0) {
            q += 1ll * cnt[i] * base;
        } else {
            q += 1ll * (total - cnt[i]) * base;
        }
        n /= 2;
        base *= 2;
    }
    return q;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        vector<i64> a(n);
        vector<int> cnt(30, 0);
        
        for (int i = 0; i < n; i++) {
            cin >> a[i];
            cal(a[i], cnt);
        }
        
        i64 ans = 0;
        for (int i = 0; i < n; i++) {
            ans = max(ans, calQ(cnt, a[i], n));
        }

        cout << ans << '\n';
    }
}

AC情况:

我的提交

标签:

分类:

更新时间: