2026.9.23 模拟赛

· · 个人记录

比赛

CF366C Dima and Salad

题意

- 美味值 $a_i

选用至少 1 个水果

要求 \dfrac{\sum\limits_{j=1}^m a_j}{\sum\limits_{j=1}^m b_j}=k

求最大美味值

n, k, a_i, b_i ∈ [1, 100]

算法分析

  1. 选择物品求最大价值,考虑 01背包

  2. 花费(消耗背包容量)

    w_i=a_j - b_j * k

    推导:\dfrac{\sum\limits_{j=1}^m a_j}{\sum\limits_{j=1}^m b_j}=k \to \sum a_j = k \times \sum b_j \to \sum (a_j - b_j * k) = 0

  3. 价值 w_i = a_i

  4. 细节 v_i ∈ \mathbb Z, 也就是说一个花费和为 j 的状态可能由负数转移而来,方便下标存储整体右移 M 个单位,将起点挪至 M

::::success[code]

#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 110;
const int M = 1e5+100;

int a[N], b[N], w[N], v[N], dp[N][M * 2];

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

    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
        cin >> b[i];
        w[i] = a[i];
        v[i] = b[i] * k - a[i];
        // v[i] = a[i] - b[i] * k;
    }

    // for (int i = 0; i < N; i++)
    //     for (int j = 0; j < M * 2; j++) dp[i][j] = -1;
    memset(dp, -1, sizeof(dp));

    dp[0][M] = 0;
    for (int i = 1; i <= n; i++)
        for (int j = M * 2 - 1; j >= 0; j--) {
            if (j - v[i] >= M * 2 - 1 || j - v[i] < 0) continue;

            if (dp[i - 1][j - v[i]] == -1) dp[i][j] = dp[i - 1][j];
            else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - v[i]] + w [i]);
        }

    if (dp[n][M]) cout << dp[n][M];
    else cout << -1;

    return 0;
}

::::

P9649 [SNCPC2019] Coolbits

题意

n$ 个区间 $[l_1, r_1], [l_2, r_2], \dots, [l_n, r_n]

从每个区间中选择一个整数,求最大与值

算法分析

  1. 按位与的性质:当前为全为 1 即为 1,任意一个为 0 则为 0
  2. 对于区间 [l, r],若左边界 l 第 i 位为 1,那区间内的所有值第 i 位都应是 1
  3. 若所有区间都满足左端 l 的第 i 位为 1,则第 i 位可以填 1
  4. 更新左端点为第 i 位为 1,其余更低位设为 0,其意义为最小合法左端点
  5. 持续判断所有位,累加答案即可

::::success[code]

#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 1e5 + 5;

int L[N], R[N], n;
ll ans;

int check(int x, int i) {
    if (!((x >> i) & 1)) x = ((x >> i) | 1) << i;

    return x;
}

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

    cin >> n;
    for (int i = 1; i <= n; i++) cin >> L[i] >> R[i];

    for (int i = 30; i >= 0; i--) {
        ll d = 1ll << i;
        bool f = 0;
        for (int j = 1; j <= n; j++)
            if (check(L[j], i) > R[j]) {
                f = 1;
                break;
            }

        if (f) continue;

        for (int j = 1; j <= n; j++) L[j] = check(L[j], i);

        ans |= d;
    }

    cout << ans;

    return 0;
}

::::

Mex Tree Gym - 103941J

题意

定义 mex 为当前连通块未出现过最小自然数

给定一棵树,问 max 恰好等于 i 的最大连通块大小

算法分析

  1. 对于根节点 0 他的 mex 含义为不包含 0 的最大连通块大小,即删去 0 的剩余连通块最大大小,也就是最大子树大小
  2. 对于其他节点 x,有解情况下就是总结点数减去子树 x 大小
  3. 无解情况:由于 v 是排列,不会出现相同数值,从 0 往下排节点数值只会更大 若从 x 往下走变小,即构成 [0, y] | y < x 的连通块必定包含 x 此情况无解
  4. 对于每一个 x 构成与 0 的最小连通块,点集为 0 \to x 的路径上的点(链),所以对于有解情况直接沿父亲 vis 染色
  5. 从小到大枚举节点,若 vis 已被染色则无解

::::success[code]

#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 1e6 + 10;

vector<int> g[N];
int a[N], sz[N], f[N];
bool vis[N];

void dfs(int x, int fa) {
    f[x] = fa;
    sz[x] = 1;
    for (auto v : g[x]) {
        if (v != fa) {
            dfs(v, x);
            sz[x] += sz[v];
        }
    }
}

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

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        a[x] = i;
    }

    for (int i = 2; i <= n; i++) {
        int x;
        cin >> x;
        g[i].push_back(x);
        g[x].push_back(i);
    }

    dfs(a[0], 0);

    int cur = a[0];
    vis[cur] = 1;

    int res = 0;
    for (auto v : g[cur]) res = max(res, sz[v]);
    cout << res << " ";

    for (int i = 1; i < n; i++) {
        int x = a[i];
        if (vis[x]) cout << 0 << " ";
        else {
            int t = x;
            while (!vis[t]) {
                vis[t] = 1;
                t = f[t];
            }

            cout << n - sz[x] << " ";
        }
    }

    cout << n;

    return 0;
}

::::

AT_abc237_g [ABC237G] Range Sort Query

题意

给定一个由 1,2,\ldots,N 组成的长度为 N 的排列 P=(P_1,P_2,\ldots,P_N),以及一个整数 X

按顺序依次处理所有查询后,请输出最终排列中满足 P_i=X 的 i

算法分析

  1. 排序操作并不好直接进行局部懒标记操作

  2. 注意到关键词 排列 和单点查询(可离线)

  3. 性质:对于小于 x 的数在序列的任意一个地方效果都是等价的(升序为例,置于 x),大于同理

  4. 于是我考虑将

    • < x$ 的数染成 $0
    • = x$ 的数染成 $1
    • > x$ 的数染成 $2
  5. 这样的三色排列的排序(升序为例)就直接按照 0 全排前面,再是 1,后面全排 2 的顺序进行区间赋值即可,降序同理

  6. 最后便利 n 个数,单点查询是否存在等于 1 的(=X)输出下标即可

  7. 细节:由于整个序列只有一个等于 X 的位置,需要特判需必须要对这种位置单点修改(下表是否需要向后推一位)WA 75pts

    
    node res = query(x, y, 1, 1, n);
    //      if (!res.b) continue;

if (opt == 1) { update(x, x + res.a - 1, 1, 1, n, 0); if (res.b) { update(x + res.a, x + res.a, 1, 1, n, 1); update(x + res.a + 1, y, 1, 1, n, 2); } else update(x + res.a, y, 1, 1, n, 2); } else { update(x, x + res.c - 1, 1, 1, n, 2); if (res.b) { update(x + res.c, x + res.c, 1, 1, n, 1); update(x + res.c + 1, y, 1, 1, n, 0); } else update(x + res.c, y, 1, 1, n, 0); }


::::success[code]
```cpp line-numbers
#include <bits/stdc++.h>

using namespace std;

#define ll long long 

const int N = 1e6 + 10;

struct node {
    int a, b, c, tag;
} tree[N];
int n, X, a[N];

void push_up(int p) {
    int l = p << 1, r = p << 1 | 1;
    tree[p].a = tree[l].a + tree[r].a;
    tree[p].b = tree[l].b + tree[r].b;
    tree[p].c = tree[l].c + tree[r].c;
}

void build(int p, int pl, int pr) {
    tree[p].tag = -1;
    if (pl == pr) {
        if (a[pl] == 0) tree[p].a = 1;
        else if (a[pl] == 1) tree[p].b = 1;
        else tree[p].c = 1;
        return;
    }

    int mid = (pl + pr) >> 1;
    build(p << 1, pl, mid);
    build(p << 1 | 1, mid + 1, pr);

    push_up(p);
}

void addtag(int p, int pl, int pr, int d) {
    tree[p].tag = d;
    if (d == 0) {
        tree[p].a = pr - pl + 1;
        tree[p].b = tree[p].c = 0;
    } else if (d == 1) {
        tree[p].b = pr - pl + 1;
        tree[p].a = tree[p].c = 0;
    } else {
        tree[p].c = pr - pl + 1;
        tree[p].a = tree[p].b = 0;
    }
}

void push_down(int p, int pl, int pr) {
    if (tree[p].tag != -1) {
        int mid = (pl + pr) >> 1;
        addtag(p << 1, pl, mid, tree[p].tag);
        addtag(p << 1 | 1, mid + 1, pr, tree[p].tag);
        tree[p].tag = -1;
    }
}

void update(int L, int R, int p, int pl, int pr, int d) {
    if (L > R) return; 

    if (L <= pl && pr <= R) {
        addtag(p, pl, pr, d); 
        return;
    }

    push_down(p, pl, pr);

    int mid = (pl + pr) >> 1;
    if (L <= mid) update(L, R, p << 1, pl, mid, d);
    if (R > mid) update(L, R, p << 1 | 1, mid + 1, pr, d);

    push_up(p);
}

node query(int L, int R, int p, int pl, int pr) {
//  cout << "PaN" << endl;
    if (L > R) return (node){0, 0, 0, 0}; 

    if (L <= pl && pr <= R) return tree[p];

    push_down(p, pl, pr);

    int mid = (pl + pr) >> 1;
    node res = {0, 0, 0, 0};
    if (L <= mid) {
        node tmp = query(L, R, p << 1, pl, mid);
        res.a += tmp.a; res.b += tmp.b; res.c += tmp.c;
    } 
    if (R > mid) {
        node tmp = query(L, R, p << 1 | 1, mid + 1, pr);
        res.a += tmp.a; res.b += tmp.b; res.c += tmp.c;
    }

    return res;
}

int main() {
//  freopen("sample.in", "r", stdin);

    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int q;
    cin >> n >> q >> X;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        if (x < X) a[i] = 0;
        else if (x == X) a[i] = 1;
        else a[i] = 2; 
    }
    //cout<<1<<"11111 "<<1<<endl; 
    build(1, 1, n);

    while (q--) {
        int opt, x, y;
        cin >> opt >> x >> y;

        node res = query(x, y, 1, 1, n);
//      if (!res.b) continue;

        if (opt == 1) {
            update(x, x + res.a - 1, 1, 1, n, 0);
            if (res.b) {
                update(x + res.a, x + res.a, 1, 1, n, 1);
                update(x + res.a + 1, y, 1, 1, n, 2);
            } else update(x + res.a, y, 1, 1, n, 2);
        } else {
            update(x, x + res.c - 1, 1, 1, n, 2);
            if (res.b) {
                update(x + res.c, x + res.c, 1, 1, n, 1);
                update(x + res.c + 1, y, 1, 1, n, 0);
            } else update(x + res.c, y, 1, 1, n, 0);
        }
    }

    for (int i = 1; i <= n; i++) 
        if (query(i, i, 1, 1, n).b == 1) {
            cout << i;
            return 0;
        }

    return 0;
} 

::::