Educational Codeforces Round 151 (Rated for Div. 2)补题A~D

发布时间 2024-01-03 12:47:45作者: jvdyvan

Educational Codeforces Round 151 (Rated for Div. 2)

A. Forbidden Integer

思路

分别处理x=1和x≠1的情况

ac代码

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;
const i64 inf = 8e18;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
const int N = 3e5 + 10;

void solve() {
    int n, k, x;
    cin >> n >> k >> x;

    if (x != 1) {
        cout << "YES\n";
        cout << n << endl;
        for (int i = 0; i < n; i++)
            cout << "1 ";
        cout << endl;
        return;
    }

    //x == 1;
    if (n % 2 == 0 && k >= 2) {
        cout << "YES\n";
        cout << n / 2 << endl;
        for (int i = 0; i < n / 2; i++)
            cout << "2 ";
        cout << endl;
        return;
    }

    if (n % 2 == 1 && k >= 3) {
        cout << "YES\n";
        cout << n / 2 << endl;
        for (int i = 0; i < n / 2 - 1; i++)
            cout << "2 ";
        cout << 3 << endl;
        return;
    }

    cout << "NO\n";

}

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

    int t = 1;
    cin >> t;
    while (t --) solve();

    return 0;
}

B. Come Together

思路

没什么好说的,直接看代码

ac代码

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;
const i64 inf = 8e18;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
const int N = 3e5 + 10;

void solve() {
    i64 a1, a2, b1, b2, c1, c2;
    cin >> a1 >> a2 >> b1 >> b2 >> c1 >> c2;

    i64 ans = 1;

    if ((a1 - b1)*(a1 - c1) > 0) {
        ans += min(abs(a1 - b1), abs(a1 - c1));
    }
    if ((a2 - b2)*(a2 - c2) > 0) {
        ans += min(abs(a2 - b2), abs(a2 - c2));
    }

    cout << ans << endl;

}

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

    int t = 1;
    cin >> t;
    while (t --) solve();

    return 0;
}

C. Strong Password

思路

遍历每位,看看能在模式式串里走得最远

ac代码

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;
const i64 inf = 8e18;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
const int N = 3e5 + 10;

void solve() {
    string m;
    int n;
    string a, b;
    cin >> m >> n >> a >> b;

    int pos = -1;
    for (int i = 0; i < n; i++) {
        int t = -1;
        int l = a[i] - '0', r = b[i] - '0';
        for (int j = l; j <= r; j++) {
            char c = j + '0';
            int p = m.find(c, pos + 1);

            if (p == -1) {
                cout << "YES\n";
                return;
            }

            t = max(t, p);
        }
        pos = t;
    }

    cout << "NO\n";

}

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

    int t = 1;
    cin >> t;
    while (t --) solve();

    return 0;
}

D. Rating System

思路

先处理出前缀和pre,后缀和suf,以及后缀和最大值max_suf。显然的,k的取值必然是某个前缀和,我们只需要遍历每个前缀和,然后计算以当前前缀和作为k时最后能拿到多少分就行。最后的得分是pre[i] + max(0, max_suf[i + 1])

ac代码

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;
const i64 inf = 8e18;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
const int N = 3e5 + 10;

void solve() {
    int n;
    cin >> n;
    std::vector<i64> a(n + 1), s(n + 1), suf(n + 2), max_suf(n + 2);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        s[i] = s[i - 1] + a[i];
    }

    for (int i = n; i >= 0; i --) {
        suf[i] = suf[i + 1] + a[i];
        max_suf[i] = max(max_suf[i + 1], suf[i]);
    }

    i64 mx = 0;
    i64 ans = 0;
    for (int i = 0; i <= n; i++) {
        i64 t = s[i] + max(0ll, max_suf[i + 1]);
        if (t > mx) {
            mx = t;
            ans = s[i];
        }
    }

    cout << ans << endl;

}

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

    int t = 1;
    cin >> t;
    while (t --) solve();

    return 0;
}