Codeforces Round #710 (Div. 3)

Codeforces Round #710 (Div. 3)

A - Strange Table

int main() {
    IOS;
    for (cin >> _; _; --_) {
        ll n, m, k; cin >> n >> m >> k;
        int y = (k - 1) / n + 1, x = (k - 1) % n + 1;
        cout << (x - 1) * m + y  << '\n';
    }
    return 0;
}

B - Partial Replacement

int ne[N], ls;
char s[55];
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> m >> s + 1; ls = k = 0;
        rep (i, 1, n) if (s[i] == '*') ne[ls] = i, ls = i;
        ne[ls] = n + 1; ++k;
        if (ne[0] == n + 1) { cout << "0\n"; continue; }
        for (int i = ne[0], j = ne[i]; j <= n; i = j, j = ne[i], ++k)
            while (ne[j] <= n && ne[j] - i <= m) j = ne[j];
        cout << k << '\n';
    }
    return 0;
}

C - Double-ended Strings

int ne[N], ls;
char a[25], b[25];
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> a + 1 >> b + 1; k = 0, n = strlen(a + 1), m = strlen(b + 1);
        rep (i, 1, n) rep (j, 1, m) {
            int c = 0; while (c < min(i, j) && a[i - c] == b[j - c]) ++c;
            umax(k, c);
        }
        cout << n + m - k * 2 << '\n';
    }
    return 0;
}

D - Epic Transformation

贪心

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; map<int, int> st;
        rep (i, 1, n) cin >> m, ++st[m]; m = 0;
        for (auto &i : st) a[++m] = i.se;
        m = *max_element(a + 1, a + 1 + m);
        cout << max(2 * m - n, n & 1) << '\n';
    }
    return 0;
}

E - Restoring the Permutation

贪心

int a[N];
set<int> v, vs;
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; rep (i, 1, n) cin >> a[i], v.insert(i), vs.insert(i); m = 1;
        rep (i, 1, n)
            if (a[i] != a[i - 1]) cout << a[i] << ' ', v.erase(a[i]);
            else cout << *v.begin() << ' ', v.erase(v.begin());
        cout << '\n';
        rep (i, 1, n)
            if (a[i] != a[i - 1]) cout << a[i] << ' ', vs.erase(a[i]);
            else {
                auto it = vs.lower_bound(a[i]); --it;
                cout << *it << ' ', vs.erase(it);
            }
        cout << '\n';
    }
    return 0;
}

F - Triangular Paths

左走列数不变,右走列数加一

每两行其中一行可以免费左走, 一行可以一直右走

模拟完事

PII a[N];
 
int main() {
    IOS; a[0] = { 1, 1 };
    for (cin >> _; _; --_) {
        cin >> n; rep(i, 1, n) cin >> a[i].fi; rep(i, 1, n) cin >> a[i].se;
        sort(a + 1, a + 1 + n); ll ans = 0;
        rep(i, 1, n) {
            ll x = a[i].se - a[i - 1].se, y = a[i].fi - a[i - 1].fi - x;
            if (a[i - 1].fi - a[i - 1].se & 1) ans += y + 1 >> 1;
            else ans += (y >> 1) + (y ? 0 : x);
        }
        cout << ans << '\n';
    }
    return 0;
}

G - Maximize the Remaining String

想要删除 s[i], 把后面最大的字母提上来

也就是 [i + 1, r] 中的字母, 保证 [i + 1, r) 的字母可以任意删除, 用线段树维护区间最大值

提上拉字母<=s[i] 那就不提, 然后按题意模拟

const int N = 2e5 + 5;
 
struct BIT {
    struct node { int l, r; char val; } tr[N * 20];
    void build(int rt, int l, int r, char* s) {
        tr[rt].l = l, tr[rt].r = r;
        if (l == r) { tr[rt].val = s[l]; return; }
        int mid = l + r >> 1;
        build(rt << 1 | 1, mid + 1, r, s); build(rt << 1, l, mid, s);
        tr[rt].val = max(tr[rt << 1].val, tr[rt << 1 | 1].val);
    }
    void change(int rt, int k) {
        if (tr[rt].l == tr[rt].r) { tr[rt].val = '\0'; return; }
        int mid = tr[rt].l + tr[rt].r >> 1;
        change(rt << 1 | (mid < k), k);
        tr[rt].val = max(tr[rt << 1].val, tr[rt << 1 | 1].val);
    }
    char ask(int rt, int l, int r) {
        if (l > r) return 0;
        if (tr[rt].l >= l && tr[rt].r <= r) return tr[rt].val;
        int mid = tr[rt].l + tr[rt].r >> 1;
        if (r <= mid) return ask(rt << 1, l, r);
        if (l > mid) return ask(rt << 1 | 1, l, r);
        return max(ask(rt << 1, l, r), ask(rt << 1 | 1, l, r));
    }
} bit;
 
int n, m, _, k, cas;
char s[N], t[N];
 
void erase(set<int>* ap, set<int>& st, char& c) {
    auto it = st.find(*ap[c - 'a'].rbegin());
    for (auto& i : ap[c - 'a']) bit.change(1, i);
    clear(ap[c - 'a']);
    if (it != st.end()) st.erase(it);
}
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        set<int> ap[26]; set<int> st; m = 0;
        cin >> s + 1; n = strlen(s + 1); bit.build(1, 1, n, s);
        rep(i, 1, n) ap[s[i] - 'a'].insert(i);
        rep(i, 0, 25) if (!ap[i].empty()) st.insert(*ap[i].rbegin());
        rep(i, 1, n) {
            if (ap[s[i] - 'a'].empty()) continue;
            if (ap[s[i] - 'a'].size() == 1) { t[++m] = s[i]; continue; }
            int cur = *st.upper_bound(i); char c = bit.ask(1, i + 1, cur);
            if (c <= s[i]) { t[++m] = s[i]; erase(ap, st, s[i]); continue; }
            while (s[i] != c) ap[s[i] - 'a'].erase(i), ++i; t[++m] = c;
            erase(ap, st, s[i]);
        }
        t[++m] = '\0'; cout << t + 1 << '\n';
    }
    return 0;
}
posted @ 2021-04-01 23:58  洛绫璃  阅读(75)  评论(0编辑  收藏  举报