Codeforces Round #706 Editorial

1496A. Split it!

類迴文判斷,只要 k = 0 或者 \(s[1,k] 和 s[n - k + 1,n]\)是迴文便可ios

特判狀況 n < 2 * k + 1NO數組

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    int _ = 1;
    for (cin >> _; _--;) {
        string s;
        int n; ll k;
        cin >> n >> k;
        cin >> s;
        bool f = true;
        for (int i = 0; i < k && f; ++i) f = s[i] == s[n - i - 1]);

        cout << (f && n >= 2 * k + 1 ? "YES\n" : "NO\n");
    }
    return 0;
}

1496B. Max and Mex

模擬,當 mex(a) < max(b) 時 必有 \(⌈\frac{a + b}2⌉ < b\) 則集合不同的數可增長一,不然每進行一次操做 + 1學習

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    int _ = 1;
    for (cin >> _; _--;) {
        int n;
        ll k;
        cin >> n >> k;
        vector<ll> a(n);
        set<ll> s;
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
            s.insert(a[i]);
        }
        sort(a.begin(), a.end());
        if (k == 0) {
            cout << s.size() << "\n";
            continue;
        }
        int i = 0;
        ll b = 0;
        while (b == a[i]) b++, i++;
        if (b <= a[n - 1]) {
            s.insert((b + a[n - 1] + 1) / 2);
            cout << s.size() << "\n";
            continue;
        }
        cout << s.size() + k << endl;
    }
    return 0;
}

1496C. Diamond Miner

將座標絕對值化存入數組排序spa

\(\sqrt{(a-c)^2 +(b - d)^2} = \sqrt{a^2 + d^2}\) 要想有最小化,只能大值匹配大值code

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    int _ = 1;
    for (cin >> _; _--;) {
        int n;
        cin >> n;
        vector<int> xx, yy;
        for (int i = 0; i < 2 * n; ++i) {
            int x, y;
            cin >> x >> y;
            if (x == 0) yy.push_back(abs(y));
            else
                xx.push_back((abs(x)));
        }
        sort(xx.begin(), xx.end());
        sort(yy.begin(), yy.end());
        double cnt = 0.0;
        for (int i = 0; i < n; ++i) {
            cnt += sqrt(1.0 * xx[i] * xx[i] + 1.0 * yy[i] * yy[i]);
        }
        cout << setprecision(15) << cnt << "\n";
    }
    return 0;
}

1496D. Let's Go Hiking

學習自 洛綾璃 dalao的思路blog

這是一道博弈題排序

因爲只能存在一條最長鏈,不然先手站一條, 後手站一條, 先手必輸ci

其次, 只有一條最長鏈, 先手和後手都會選在最長鏈上, 不然誰不在, 另外一方直接獲勝get

在其 先手會在山峯, 不然後手直接卡死string

故先手會選擇在 最長鏈的最高端, 後手會選擇最長鏈最遠的地方, 保證和先手相隔 偶數個位置(保證二者都走最長鏈, 後手勝)

後手保證了先手最長鏈必定會輸, 只能走最長鏈的反方向, 比較先手和後手能走的長度, 判斷是否能先手贏

const int N = 1e5 + 5;
int t, n, maxn, ans, a[N], p1[N], p2[N];
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", a + i);
        p1[i] = (a[i] <= a[i - 1] || i == 1) ? 0 : (p1[i - 1] + 1);
        maxn = max(maxn, p1[i]);
    }
    for (int i = n; i >= 1; i--)
        p2[i] = (a[i] <= a[i + 1] || i == n) ? 0 : (p2[i + 1] + 1),
        maxn = max(p2[i], maxn);
    for (int i = 1; i <= n; i++)
        if (p1[i] == p2[i] && p1[i] == maxn && maxn > 0 && ((maxn & 1) == 0)) {
            ans = i;
            break;
        }
    for (int i = 1; i <= n; i++)
        if (ans != i && (p1[i] == maxn || p2[i] == maxn)) {
            ans = 0;
            break;
        }
    printf("%d", ans ? 1 : 0);
}
相關文章
相關標籤/搜索