Codeforces Round #727 (Div. 2)

Codeforces Round #727 (Div. 2)

A - Contest Start

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    for (cin >> _; _; --_) {
        long long x, y, z, ans = 0; cin >> x >> y >> z;
        y = min(z / y, x - 1); ans = (x - y) * y + (y - 1) * y / 2;
        cout << ans << '\n';
    }
    return 0;
}

B - Love Song

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m >> s + 1;
    for (int i = 1; i <= n; ++i) {
        memcpy(c[i], c[i - 1], sizeof c[i]);
        ++c[i][s[i] - 'a'];
    }
    while (m--) {
        int l, r, ans = 0; cin >> l >> r;
        for (int i = 0; i < 26; ++i) ans += (c[r][i] - c[l - 1][i]) * (i + 1);
        cout << ans << '\n';
    }
    return 0;
}

D - PriceFixed

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i].second >> a[i].first;
    sort(a, a + n);
    for (int i = 0, j = n - 1; i <= j; ++i) {
        if (a[i].second == 0) continue;
        while (c < a[i].first && i <= j) {
            long long d = a[i].first - c;
            if (a[j].second == 0) --j;
            if (d <= a[j].second) {
                a[j].second -= d;
                ans += d << 1;
                c += d;
            } else {
                ans += a[j].second << 1;
                c += a[j].second;
                a[j--].second = 0;
            }
        }
        c += a[i].second;
        ans += a[i].second;
    }
    cout << ans;
    return 0;
}

F - Strange Array

分两种情况

  1. \(a_i\) 位于区间[l,r]的右半边
  2. \(a_i\) 位于区间[l,r]的左半边

设cntL, cntR, cntE 分别表示区间[l,r]内小于\(a_i\), 大于\(a_i\), 等于\(a_i\) 的数的个数

则, 对于

  1. \(ans = \left \lfloor \frac{cntR + cntE - cntL}{2} \right \rfloor\)
  2. \(ans = \left \lfloor \frac{cntL + cntE - 1 - cntR}{2} \right \rfloor\)

我们用线段树维护\(cntL - cntR\) or \(cntR - cntL\) 区间最大值即可

struct BIT {
    struct node { int lv, rv, v; } tr[N * 17];
    node merge(node a, node b) {
        return { max(a.lv, a.v + b.lv), max(b.v + a.rv, b.rv), a.v + b.v };
    }
    void build(int p, int l, int r) {
        if (l == r) {
            tr[p].lv = tr[p].rv = tr[p].v = 1;
            return;
        }
        int mid = l + r >> 1;
        build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r);
        tr[p] = merge(tr[p << 1], tr[p << 1 | 1]);
    }
    void change(int p, int l, int r, int k, int v) {
        if (l == r) { tr[p] = { v, v, v }; return; }
        int mid = l + r >> 1;
        if (k <= mid) change(p << 1, l, mid, k, v);
        else change(p << 1 | 1, mid + 1, r, k, v);
        tr[p] = merge(tr[p << 1], tr[p << 1 | 1]);
    }
    node ask(int p, int l, int r, int L, int R) {
        if (R < l || L > r || L > R) return { 0, 0, 0 };
        if (l >= L && r <= R) return tr[p];
        int mid = l + r >> 1;
        if (R <= mid) return ask(p << 1, l, mid, L, R);
        if (mid < L) return ask(p << 1 | 1, mid + 1, r, L, R);
        return merge(ask(p << 1, l, mid, L, R), ask(p << 1 | 1, mid + 1, r, L, R));
    }
} T;

int n, m, k, _;
int ans[N];
pair<int, int> a[N];

void work(bool f) {
    T.build(1, 0, n - 1);
    for (int i = 0, j = 1; i < n;) {
        while (j <= n && a[j].first == a[i].first) ++j;
        for (int k = i; k < j; ++k) {
            BIT::node l = T.ask(1, 0, n - 1, 0, a[k].second);
            BIT::node r = T.ask(1, 0, n - 1, a[k].second + 1, n - 1);
            ans[a[k].second] = max(ans[a[k].second],
                max(0, l.rv) + max(0, r.lv) - f >> 1);
        }
        for (; i < j; ++i) T.change(1, 0, n - 1, a[i].second, -1);
    }
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i].first, a[i].second = i;
    sort(a, a + n); work(0);
    reverse(a, a + n); work(1);
    for (int i = 0; i < n; ++i) cout << ans[i] << ("\n "[i < n]);
    return 0;
}
posted @ 2021-06-28 13:40  洛绫璃  阅读(47)  评论(0编辑  收藏  举报