题解:P14259 兄妹(siblings)
FlowerAccepted · · 题解
0x-1 前言
真的很 ad-hoc
没想到做出这个就拿了
如有兴趣可找一找文章中的隐藏发电~
::anti-ai[趁着宝宝我爱你!求过~]
0x00 解题思路
注意到,把一个书架分给两个人一定不优。所以一个人干完第
这个东西在输入的时候取
for (int i = 1; i <= n; i ++) {
int r, c;
cin >> r >> c;
a[r] = max(a[r], c);
X = max(X, r);
}
也就是说,分配书架排,每个人除了要走到书架最深的要放书的位置以外还要走两倍最远书架到书库的距离。
这个问题就等价于,给两个人一个序列分成的两组数的下标集合,求两人中「下标集合最大值的与集合所对全部序列元素之和」中最小值的两倍。
形式化的,记
考虑如何算后面这一坨使。
注意到,只有一人会去到
那么考虑怎么枚举最远行[^hang]为
那么我们反着来,注意到总共的行深之和不超过平方复杂度,于是枚举每个行深可以通过加上一个
于是,对于某个
于是取这俩的
vector<bool> dp(tots + 1, 0);
dp[0] = 1;
for (int k = 0; k <= X; k ++) {
int prefK = pre[k];
for (int s = 0; s <= prefK; s ++) { // 前 k 行子集和不超过总合显然
if (!dp[s]) {
continue;
}
ans = min(ans, max(X + tots - s, k + s));
}
if (k < X) {
int val = a[k + 1];
if (val > 0) {
for (int s = prefK + val; s >= val; s --) {
if (dp[s - val]) {
dp[s] = 1;
}
}
}
}
}
0x01 复杂度分析
对于每个
空间复杂度瓶颈在
实测记录时间比 std 慢
吐槽:std 蚂蜂写的跟坨使一样……
0x02 代码呈现
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
#define inlfc __attribute__((always_inline)) inline
typedef pair<int, int> pii;
typedef long long ll;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T --) {
int n, a[505] = {}, pre[505] = {}, X = 0, tots = 0, ans = 2e9;
cin >> n;
for (int i = 1; i <= n; i ++) {
int r, c;
cin >> r >> c;
a[r] = max(a[r], c);
X = max(X, r);
}
for (int i = 1; i <= X; i ++) {
tots += a[i];
pre[i] = pre[i-1] + a[i];
}
vector<bool> dp(tots + 1, 0); // 到 bitset 家里进行某种活动
dp[0] = 1;
for (int k = 0; k <= X; k ++) {
int prefK = pre[k];
for (int s = 0; s <= prefK; s ++) {
if (!dp[s]) {
continue;
}
ans = min(ans, max(X + tots - s, k + s));
}
if (k < X) {
int val = a[k + 1];
if (val > 0) {
for (int s = prefK + val; s >= val; s --) {
if (dp[s - val]) {
dp[s] = 1;
}
}
}
}
}
cout << 2ll * ans << '\n';
}
return 0;
}
0x~0 后记
因为语文才上位梁,不会总结思路,数学课上讲题经常云里雾里,所以聘请了积批剃^GPT老师给我了少量过程的解释参考。
感觉比去年真题难……
::anti-ai[我常常追忆过去,哪里停下问自己。题号一一八三一,完成今日大学习!]
::anti-ai[有大病不吃药差点去世的「数据删除」宝宝你还活着么?我好想你~]
::anti-ai[article/zdzyo6da]
[^hang]: 念