CodeCraft-22 and Codeforces Round #795 (Div. 2)

· · 个人记录

CF 795 div2(id: 1691)

CodeCraft-22 and Codeforces Round #795 (Div. 2)

CF1691A

奇数+奇数=偶数,偶数+偶数=偶数

// Problem: A. Beat The Odds
// From: Codeforces - CodeCraft-22 and Codeforces Round #795 (Div. 2)
// URL: https://codeforces.com/contest/1691/problem/A
// Time: 2022-05-31 22:35
// Author: lingfunny

#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5+10;

int n, a[mxn];

inline void solve() {
    int c1 = 0, c2 = 0;
    scanf("%d", &n);
    for(int i = 1, x; i <= n; ++i) {
        scanf("%d", &x);
        if(x&1) ++c1;
        else ++c2;
    }
    printf("%d\n", min(c1, c2));
}

int main() {
    int tt;
    scanf("%d", &tt);
    while(tt--) solve();
}

CF1691B

相同权值的必须有两个及以上。多测记得清空。

// Problem: B. Shoe Shuffling
// From: Codeforces - CodeCraft-22 and Codeforces Round #795 (Div. 2)
// URL: https://codeforces.com/contest/1691/problem/B
// Time: 2022-05-31 22:35
// Author: lingfunny

#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5+10;

int n, a[mxn], s[mxn];

inline void solve() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d", a+i);
    int tot = 0, c = 1;
    a[n+1] = 0;
    for(int i = 1; i <= n; ++i)
    if(a[i] == a[i+1]) ++c;
    else s[++tot] = c, c = 1;
    if(*min_element(s+1, s+tot+1) == 1) return puts("-1"), void();
    else {
        int sum = 0;
        for(int i = 1; i <= tot; ++i) {
            sum += s[i];
            printf("%d ", sum);
            for(int k = 1; k < s[i]; ++k)
            printf("%d ", sum-s[i]+k);
        }
    }
    puts("");
}

int main() {
    int tt;
    scanf("%d", &tt);
    while(tt--) solve();
}

CF1691C

每个 \texttt{1} 固定贡献 11,移到最右可以减少 10,最左可以减少 1,贪心即可。

// Problem: C. Sum of Substrings
// From: Codeforces - CodeCraft-22 and Codeforces Round #795 (Div. 2)
// URL: https://codeforces.com/contest/1691/problem/C
// Time: 2022-05-31 22:35
// Author: lingfunny

#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5+10;

int n, k, ans;
char s[mxn];

inline void solve() {
    scanf("%d%d", &n, &k);
    scanf("%s", s+1);
    int L = 0, R = 0;
    ans = 0;
    for(int i = 1; i <= n; ++i) ans += (s[i] - '0') * 11;
    for(int i = 1; i <= n; ++i) if(s[i] == '1') { L = i; break; }
    for(int i = n; i >= 1; --i) if(s[i] == '1') { R = i; break; }
    if(L == 0) puts("0");
    else if(L == R) printf("%d\n", 11-(k >= n-R?10: k >= L-1?1:0));
    else {
        if(k >= n-R) k -= n-R, ans -= 10;
        if(k >= L-1) k -= L-1, ans--;
        printf("%d\n", ans);
    }
}

int main() {
    int tt;
    scanf("%d", &tt);
    while(tt--) solve();
}

CF1691D

枚举 \max 然后判断。

// Problem: D. Max GEQ Sum
// From: Codeforces - CodeCraft-22 and Codeforces Round #795 (Div. 2)
// URL: https://codeforces.com/contest/1691/problem/D
// Time: 2022-05-31 22:35
// Author: lingfunny

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int mxn = 2e5+10;

int n, a[mxn], tot, pos[mxn], lg[mxn], p[mxn], L[mxn], R[mxn];
LL s[mxn], mis[20][mxn], mxs[20][mxn];
inline LL Qmi(int L, int R) {
    int k = lg[R-L+1];
    return min(mis[k][L], mis[k][R-(1<<k)+1]);
}
inline LL Qmx(int L, int R) {
    int k = lg[R-L+1];
    return max(mxs[k][L], mxs[k][R-(1<<k)+1]);
}

inline void solve() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d", a+i), s[i] = s[i-1] + a[i], p[i] = i, L[i] = i-1, R[i] = i+1;
    for(int i = 1; i <= n; ++i) mis[0][i] = s[i-1];
    for(int i = 1; i <= n; ++i) mxs[0][i] = s[i];
    for(int i = 1; i <= lg[n]; ++i)
    for(int j = 1; j <= n - (1<<i) + 1; ++j)
    mis[i][j] = min(mis[i-1][j], mis[i-1][j+(1<<(i-1))]),
    mxs[i][j] = max(mxs[i-1][j], mxs[i-1][j+(1<<(i-1))]);
    sort(p+1, p+n+1, [&](int x, int y) { return a[x] < a[y]; });
    for(int i = 1; i <= n; ++i) {
        int u = p[i];
        R[L[u]] = R[u];
        L[R[u]] = L[u];
        if(a[u] < Qmx(u, R[u]-1) - Qmi(L[u]+1, u)) return puts("NO"), void();
    }
    puts("YES");
}

int main() {
    lg[0] = -1;
    for(int i = 1; i < mxn; ++i) lg[i] = lg[i>>1] + 1;
    int tt;
    scanf("%d", &tt);
    while(tt--) solve();
}

CF1691E link

首先用并查集维护,从左到右对于每个端点处理。

对于同一个连通块,只保留同种颜色最右边的那个,然后对于新加的点,可以对相反颜色造成一个联通块的贡献,贡献完后可以把非最右端点的线段删掉,这样每个线段只会贡献 O(1)。

可能讲得很不清楚,跟在口胡一样,因为我现在脑子不太清醒。

// Problem: E. Number of Groups
// From: Codeforces - CodeCraft-22 and Codeforces Round #795 (Div. 2)
// URL: https://codeforces.com/contest/1691/problem/E
// Time: 2022-05-31 22:35
// Author: lingfunny

#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e5+10;

struct seg { int l, r, c; } s[mxn];
int pos[mxn], tot, n, fa[mxn];
vector <int> Begin[mxn], End[mxn];
int find(int x) { return fa[x] < 0 ? x : fa[x] = find(fa[x]); }
inline void merge(int x, int y) {
    int rx = find(x), ry = find(y);
    if(rx == ry) return; else if(fa[rx] > fa[ry]) swap(rx, ry);
    fa[rx] += fa[ry]; fa[ry] = rx;
}
set <int> S[2];

inline void solve() {
    scanf("%d", &n); tot = 0;
    for(int i = 1; i <= n; ++i) scanf("%d%d%d", &s[i].c, &s[i].l, &s[i].r), pos[++tot] = s[i].l, pos[++tot] = s[i].r;
    sort(pos+1, pos+tot+1); tot = unique(pos+1, pos+tot+1) - pos - 1;
    for(int i = 1; i <= tot; ++i) vector <int> ().swap(Begin[i]), vector <int> ().swap(End[i]);
    for(int i = 1; i <= n; ++i) {
        s[i].l = lower_bound(pos+1, pos+tot+1, s[i].l) - pos;
        s[i].r = lower_bound(pos+1, pos+tot+1, s[i].r) - pos;
        Begin[s[i].l].push_back(i);
        End[s[i].r].push_back(i);
        fa[i] = -1;
    }
    for(int i = 1; i <= tot; ++i) {
        for(int p: Begin[i]) {
            int _c = s[p].c^1, res = -1;
            for(int x: S[_c]) {
                merge(x, p);
                if(res == -1 || s[x].r > s[res].r) res = x;
            }
            if(res > 0) S[_c].clear(), S[_c].insert(res);
            S[_c^1].insert(p);
        }
        for(int p: End[i]) S[s[p].c].erase(p);
    }
    int ans = 0;
    for(int i = 1; i <= n; ++i) ans += fa[i] < 0;
    printf("%d\n", ans);
}

int main() {
    int tt;
    scanf("%d", &tt);
    while(tt--) solve();
} 

CF1691F link

upd: 看起来是一题很水的换根 dp。一定补。

upd: 做了。

不难想到换根 dp,然后我们可以写出如下式子:

f_u=\left(\dbinom{\mathrm{sz}_u}{k}-\sum_{v}\dbinom{\mathrm{sz}_v}{k}\right)\mathrm{sz}_u+\sum_{v}f_v

具体含义就是:考虑这 k 个点,什么时候 \operatorname{lca} 为 u。

减法原理,考虑什么时候不为 u,当且仅当这 k 个点来自于同一颗子树。减去\sum\limits_{v}\dbinom{\mathrm{sz}_v}{k} 即可。

最后把子树答案合并上去即可。

观察这个式子,可以拆出「只和 v 有关」、「只和 u 有关」、「和 u,v 都有关」三种。为了好换根,我们需要让和 v 有关的都比较好统计。于是考虑重定义 f_u 和 g_u。f_u 表示所有儿子的答案,g_u 表示 \sum\limits_{v}\dbinom{\mathrm{sz}_v}{k}。

此时子树 u 的答案应该是:

\mathrm{ans}_u=\left(\dbinom{\mathrm{sz}_u}{k}-g_u\right)\mathrm{sz}_u+f_u

此时换根只需要修改 g 和 f 即可,减减加加上面提到的组合数,相信大家都会。

没什么注意事项,没有细节,很好写。

码

// Problem: F. K-Set Tree
// From: Codeforces - CodeCraft-22 and Codeforces Round #795 (Div. 2)
// URL: https://codeforces.com/contest/1691/problem/F
// Time: 2022-05-31 22:35
// Author: lingfunny

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int mxn = 2e5+10, mod = 1e9+7;
int jc[mxn], ijc[mxn], n, f[mxn], g[mxn], k, sz[mxn], ans;
// f: 子树 ans 和
// g: 子树 (sz, k) 和
// ans[u]: (C(S-1, k-1) + C(S-1, k) - g[u]) * S + f[u]
vector <int> G[mxn];

inline int mul() { return 1; }
template <typename... A>
int mul(int x, A... a) { return (LL)x*mul(a...)%mod; }
inline int qpow(int x, int y) {
    int res = 1;
    for(; y; x = mul(x, x), y >>= 1) if(y&1) res = mul(res, x);
    return res;
}
inline int C(int n, int m) {
    if(m > n) return 0;
    return mul(jc[n], ijc[m], ijc[n-m]);
}
void add(int &x) { x = x; }
template <typename... A>
void add(int &x, int y, A... a) { x += (x + y) >= mod ? y - mod : y; add(x, a...); }
inline int calc(int S, int F, int G) { return (mul((C(S, k) - G + mod)%mod, S) + F) % mod; }
void dfs(int u, int fa) {
    sz[u] = 1;
    for(int v: G[u]) if(v != fa) {
        dfs(v, u), sz[u] += sz[v];
        add(f[u], calc(sz[v], f[v], g[v]));
        add(g[u], C(sz[v], k));
    }
}
void dfs2(int u, int fa) {
    if(fa) {
        int F = f[fa], G = g[fa];
        add(F, mod - calc(sz[u], f[u], g[u])), add(G, mod - C(sz[u], k));
        add(f[u], calc(n - sz[u], F, G));
        add(g[u], C(n - sz[u], k));
    }
    add(ans, calc(n, f[u], g[u]));
    for(int v: G[u]) if(v != fa) dfs2(v, u);
}

signed main() {
    scanf("%d%d", &n, &k);
    jc[0] = 1; for(int i = 1; i <= n; ++i) jc[i] = mul(jc[i-1], i);
    ijc[n] = qpow(jc[n], mod-2); for(int i = n - 1; ~i; --i) ijc[i] = mul(ijc[i+1], i+1);
    for(int i = 1, u, v; i < n; ++i) scanf("%d%d", &u, &v), G[u].emplace_back(v), G[v].emplace_back(u);
    dfs(1, 0); dfs2(1, 0);
    printf("%d\n", ans);
    return 0;
}

我 tm 在梦游吗,掉大分