Codeforces Round 1032 (Div. 3) Разбор

Правка ru1, от voventa, 2025-06-16 10:30:42

Всем спасибо за участие! 👊👊👊

2121A - Letter Home

Tutorial is loading...
#include<bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, s;
        cin >> n >> s;
        vector<int> x(n);
        for (int i = 0; i < n; i++) cin >> x[i];
        int ans = min(abs(s - x[0]), abs(s - x.back())) + x.back() - x[0];
        cout << ans << '\n';
    }
    return 0;
}

2121B - Above the Clouds

Tutorial is loading...
#include<bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        string s;
        cin >> s;
        vector<int> cnt(26, 0);
        for (auto c : s) cnt[c - 'a']++;
        int flag = 0;
        for (int i = 0; i < 26; i++) {
            if (cnt[i] >= 3) flag = 1;
            else if (cnt[i] == 2 && (s[0] - 'a' != i || s.back() - 'a' != i)) flag = 1;
        }
        if (flag) cout << "Yes" << '\n';
        else cout << "No" << '\n';
    }
    return 0;
}

2121C - Those Who Are With Us

Tutorial is loading...
#include<bits/stdc++.h>

using namespace std;

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        vector<vector<int>> a(n, vector<int> (m));
        int mx = 0, cnt_mx = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> a[i][j];
                if (a[i][j] > mx) {
                    mx = a[i][j], cnt_mx = 1;
                } else if (a[i][j] == mx) {
                    cnt_mx++;
                }
            }
        }
        vector<int> r(n), c(m);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (a[i][j] == mx) {
                    r[i]++;
                    c[j]++;
                }
            }
        }
        int flag = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (r[i] + c[j] - (a[i][j] == mx) == cnt_mx) {
                    flag = 1;
                }
            }
        }
        cout << mx - flag << '\n';
    }
}

2121D - 1709

Tutorial is loading...
#include<bits/stdc++.h>

using namespace std;

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> a(n), b(n);
        for (int i = 0; i < n; i++) cin >> a[i];
        for (int i = 0; i < n; i++) cin >> b[i];
        vector<pair<int, int>> ans;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < n; j++) {
                if (a[j - 1] > a[j]) {
                    swap(a[j - 1], a[j]);
                    ans.push_back({1, j});
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < n; j++) {
                if (b[j - 1] > b[j]) {
                    swap(b[j - 1], b[j]);
                    ans.push_back({2, j});
                }
            }
        }
        for (int i = 0; i < n; i++) {
            if (a[i] > b[i]) {
                ans.push_back({3, i + 1});
            }
        }
        cout << ans.size() << '\n';
        for (auto [x, y] : ans) cout << x << " " << y << '\n';
    }
}

2121E - Sponsor of Your Problems

Tutorial is loading...
#include<bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        string l, r;
        cin >> l >> r;
        if (l == r) {
            cout << 2 * l.size() << '\n';
            continue;
        }
        int ptr = 0;
        while (ptr < l.size() && l[ptr] == r[ptr]) ptr++;
        if (l[ptr] + 1 < r[ptr]) {
            cout << 2 * ptr << '\n';
        } else {
            int res = 2 * ptr + 1;
            for (int i = ptr + 1; i < l.size(); i++) {
                if (l[i] == '9' && r[i] == '0') res++;
                else break;
            }
            cout << res << '\n';
        }
    }
    return 0;
}

2121F - Yamakasi

Tutorial is loading...
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
int const maxn = 2e5 + 5;
int a[maxn];
ll pref[maxn], s;

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, x;
        cin >> n >> s >> x;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            pref[i] = pref[i - 1] + a[i];
        }
        ll ans = 0;
        map<ll, int> cnt;
        int lef = 1;
        for (int r = 1; r <= n; r++) {
            if (a[r] > x) cnt.clear(), lef = r + 1;
            else if (a[r] == x) {
                while (lef <= r) {
                    cnt[pref[lef - 1]]++;
                    lef++;
                }
            }
            ans += cnt[pref[r] - s];
        }
        cout << ans << '\n';
    }
    return 0;
}

2121G - Gangsta

Tutorial is loading...
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
int const maxn = 2e5 + 5;
int pref[maxn];

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        string s;
        cin >> s;
        ll ans = 0;
        for (int i = 0; i < n; i++) {
            pref[i + 1] = pref[i];
            if (s[i] == '0') pref[i + 1]--;
            else pref[i + 1]++;
        }
        for (int i = 1; i <= n; i++) {
            ans += (ll)i * (n - i + 1);
        }
        sort(pref, pref + n + 1);
        for (int i = 0; i <= n; i++) {
            ans += (ll)pref[i] * (i - (n - i));
        }
        cout << ans / 2 << '\n';
    }
    return 0;
}

2121H - Ice Baby

Tutorial is loading...
#include<bits/stdc++.h>

using namespace std;

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        multiset<int> dp;
        for (int i = 1; i <= n; i++) {
            int l, r;
            cin >> l >> r;
            auto it = dp.upper_bound(r);
            if (it != dp.end()) {
                dp.erase(it);
            }
            dp.insert(l);
            cout << dp.size() << " ";
        }
        cout << '\n';
    }
}

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en4 Английский Vladosiya 2025-06-17 20:11:03 18 Tiny change: '1F44A;\n\n[probl' -> '1F44A;\n\n\n[probl'
ru7 Русский Vladosiya 2025-06-17 20:09:17 14
ru6 Русский Vladosiya 2025-06-17 20:07:57 2 Мелкая правка: 'Разбор">\n[tutoria' -> 'Разбор">\n\n[tutoria'
en3 Английский voventa 2025-06-17 19:57:08 0 (published)
ru5 Русский voventa 2025-06-17 19:56:47 0 (опубликовано)
ru4 Русский voventa 2025-06-17 19:50:52 2
en2 Английский voventa 2025-06-16 10:47:47 15
en1 Английский voventa 2025-06-16 10:45:57 7595 Initial revision for English translation (saved to drafts)
ru3 Русский voventa 2025-06-16 10:40:09 650
ru2 Русский voventa 2025-06-16 10:31:51 45
ru1 Русский voventa 2025-06-16 10:30:42 6898 Первая редакция (сохранено в черновиках)