跳到正文

题解

题解:P1675 [USACO05FEB] Jersey Politics G

CuteMurasame约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年7月3日
收录
2026年7月3日
更新
2026年11月17日
篇幅
约 7 分钟
题目
P1675

版本

  1. v1当前

本题解为确定性算法,由 ChatGPT 5.5 Thinking 编写,本人进行了一些格式化。

关键观察

设每组需要超过 T=500kT=500k 头 Jersey 的奶牛。

如果存在一组可行划分,那么一定存在两个获胜组,它们一共包含 2k2k 个城市,且这两个组的 Jersey 奶牛数都严格大于 TT。

我们把所有城市按 wiw_i 从大到小排序,取前 2k2k 个城市,记为集合 QQ。

结论是:

如果原问题有解,那么这前 2k2k 个城市也一定能被分成两个大小为 kk 的组,并且两组的 Jersey 奶牛数都严格大于 TT。

所以问题变成:

在前 2k2k 个城市里,找一个大小为 kk 的子集,使它的权值和为 xx,满足 T<x<S−TT < x < S-T。其中 SS 是前 2k2k 个城市的权值总和。

这样这个子集作为第一组,剩下的前 2k2k 个城市作为第二组,剩余 kk 个城市作为第三组即可。

正确性证明

假设原问题有解,存在两个满足条件的组 A,BA,B,每组 kk 个城市,且 ∑A>T,∑B>T\sum A >T,\sum B >T。

令 P=A∪BP=A\cup B,则 PP 是某 2k2k 个城市的集合。

把 PP 中城市权值从大到小排序为 p1≥p2≥⋯≥p2kp_1\ge p_2\ge \cdots \ge p_{2k},把所有城市中权值最大的前 2k2k 个城市排序为 q1≥q2≥⋯≥q2kq_1\ge q_2\ge \cdots \ge q_{2k}。由于 q1,…,q2kq_1,\dots,q_{2k} 是全体城市中最大的 2k2k 个,所以对任意 rr 都有 qr≥prq_r\ge p_r。

现在保留 PP 中每个位置原来属于 AA 还是 BB 的标签。

如果 prp_r 属于 AA,就把 qrq_r 分到新组 A′A';如果 prp_r 属于 BB,就把 qrq_r 分到新组 B′B'。

因为原来 A,BA,B 都有 kk 个城市,所以新组 A′,B′A',B' 也都恰好有 kk 个城市。并且 ∑A′≥∑A>T\sum A' \ge \sum A>T,∑B′≥∑B>T\sum B'\ge \sum B>T,所以前 2k2k 个城市一定可以分成两个满足条件的组。

接下来,只要在前 2k2k 个城市中找到一个大小为 kk 的子集,设其权值和为 xx。它的补集也有 kk 个城市,权值和为 S−xS-x。

要求两组都获胜,即 x>Tx>T 且 S−x>TS-x>T,也就是 T<x<S−TT<x<S-T,由于题目保证有解,根据上面的证明,这样的子集一定存在。

DP 做法

在前 2k2k 个城市中做 0/1 背包。

定义 dpc,sdp_{c,s} 表示能否从已经考虑的城市中选出 cc 个城市,使权值和为 ss。

为了方便回溯,用前缀 DP,prei,c,spre_{i,c,s} 表示只考虑前 ii 个候选城市,是否能选出 cc 个城市,权值和为 ss。

最后找一个 ss,满足 500k+1≤s≤S−500k−1500k+1 \le s \le S-500k-1 并且 pre2k,k,s=truepre{2k,k,s}=\texttt{true},然后回溯得到第一组。

复杂度

前 2k≤1202k\le 120,每组最多 k≤60k\le 60,单组权值和最多 1000k≤600001000k\le 60000。

时间复杂度 O(2k⋅k⋅1000k)O(2k\cdot k\cdot 1000k),使用 bitset 后非常快。

空间复杂度约为 O(2k⋅k⋅1000k)O(2k\cdot k\cdot 1000k),按 bit 存储,约 55MB。

C++17 代码

C++
#include <bits/stdc++.h>
using namespace std;

const int MAXK = 60;
const int MAXN = 120;
const int MAXS = 60000;

bitset<MAXS + 1> pre[MAXN + 1][MAXK + 1];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int k;
    cin >> k;

    int n = 3 * k;
    vector<pair<int, int>> a(n);

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

    sort(a.begin(), a.end(), [](auto &x, auto &y) {
        if (x.first != y.first) return x.first > y.first;
        return x.second < y.second;
    });

    int m = 2 * k;
    int maxSum = 1000 * k;
    int T = 500 * k;

    int totalTop = 0;
    for (int i = 0; i < m; i++) {
        totalTop += a[i].first;
    }

    pre[0][0][0] = 1;

    for (int i = 1; i <= m; i++) {
        int w = a[i - 1].first;

        for (int c = 0; c <= k; c++) {
            pre[i][c] = pre[i - 1][c];
        }

        for (int c = 1; c <= k; c++) {
            pre[i][c] |= (pre[i - 1][c - 1] << w);
        }
    }

    int L = T + 1;
    int R = totalTop - T - 1;
    R = min(R, maxSum);

    int target = -1;

    for (int s = L; s <= R; s++) {
        if (pre[m][k][s]) {
            target = s;
            break;
        }
    }

    // 题目保证有解,所以正常情况下一定能找到
    if (target == -1) {
        return 0;
    }

    vector<int> group1, group2, group3;
    vector<int> used(m, 0);

    int c = k;
    int s = target;

    for (int i = m; i >= 1; i--) {
        int w = a[i - 1].first;

        if (pre[i - 1][c][s]) {
            continue;
        } else {
            used[i - 1] = 1;
            group1.push_back(a[i - 1].second);
            s -= w;
            c--;
        }
    }

    for (int i = 0; i < m; i++) {
        if (!used[i]) {
            group2.push_back(a[i].second);
        }
    }

    for (int i = m; i < n; i++) {
        group3.push_back(a[i].second);
    }

    for (int x : group1) {
        cout << x << '\n';
    }
    for (int x : group2) {
        cout << x << '\n';
    }
    for (int x : group3) {
        cout << x << '\n';
    }

    return 0;
}

这个算法是确定性的,且由于题目保证有解,最终一定能找到合法划分。

原文