题解:P10010 [集训队互测 2023] Grievous Lady

· · 题解

题目传送门

\mathtt{My \ Blog}——题解:P10010 [集训队互测 2023] Grievous Lady

注意到几乎所有这道题的题解都是搜索+贪心或 Local search^1。

本题正解

对于这种一看就没有思路的题目,我们可以用随机化骗分。

首先随机一个顺序,然后让一开始的 S 为 1 ~ \displaystyle\lfloor\frac{n}{2}\rfloor(因为随机,所以最终答案的 |S| 应该是 \displaystyle\lfloor\frac{n}{2}\rfloor 左右)。

后面做下面的操作若干次。

  1. 随机选择一个编号
  2. 如果这个编号在 S 里,把这个编号从 S 里去除,否则把这个编号加入 S。
  3. 如果答案更优,更新答案,否则撤回刚才的 2. 操作

还有,就是怎么写 mt19937 的 seed(代码高亮部分),可以参考 这个 \mathtt{Blog} 和 另一个 \mathtt{Blog}。

俗话说的好

代码五分钟,调参五小时

:::info[\large{\mathbf{Code}}]{open} AC 记录

如果过不了,多交几遍就可以了。(逃

#include <bits/stdc++.h>
using namespace std;‌‍
typedef long long LL;‌‍
typedef __int128 i128;‌‍
const int N = 3e3 + 10;‌‍
int T, n, p[N];‌‍
LL A, B, a[N], b[N];‌‍
i128 suma, sumb, ans;‌‍
bool is_in[N];‌‍
template<typename T> inline void read(T &x) {‌‍‌‍
    x = 0;‌‍ T f = 1;‌‍ char ch = getchar();‌‍
    while(!isdigit(ch)) {‌‍‌‍ if(ch == '-') f = -1;‌‍ ch = getchar();‌‍ }‌‍‌‍
    while(isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();‌‍
    x *= f;‌‍
}‌‍‌‍
template<typename T, typename... Args> inline void read(T &x, Args &...args) {‌‍‌‍
    read(x), read(args...);‌‍
}‌‍‌‍
void print(i128 x) {‌‍‌‍
    if(x > 9) print(x / 10);‌‍
    putchar(x % 10 + '0');‌‍
}‌‍‌‍
seed_seq seq{‌‍‌‍
    (uint64_t) chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count(),
    (uint64_t) chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count(),
    (uint64_t) __builtin_ia32_rdtsc(),
    (uint64_t) new char
}‌‍‌‍;‌‍
mt19937 rnd(seq);‌‍
void local_search() {‌‍‌‍
    for(int i = 0;‌‍ i < 62500;‌‍ i ++) {‌‍‌‍
        int r = rnd() % n + 1;‌‍
        if(is_in[p[r]]) {‌‍‌‍ // is in B
            if((suma + a[p[r]]) * (sumb - b[p[r]]) > ans) {‌‍‌‍
                suma += a[p[r]];‌‍
                sumb -= b[p[r]];‌‍
                ans = suma * sumb;‌‍
                is_in[p[r]] = false;‌‍
            }‌‍‌‍
        }‌‍‌‍
        else {‌‍‌‍ // is in A
            if((suma - a[p[r]]) * (sumb + b[p[r]]) > ans) {‌‍‌‍
                suma -= a[p[r]];‌‍
                sumb += b[p[r]];‌‍
                ans = suma * sumb;‌‍
                is_in[p[r]] = true;‌‍
            }‌‍‌‍
        }‌‍‌‍
    }‌‍‌‍
}‌‍‌‍
void solve() {‌‍‌‍
    i128 res = 0;‌‍
    for(int i = 1;‌‍ i <= n;‌‍ i ++) {‌‍‌‍
        read(a[i], b[i]);‌‍
        p[i] = i;‌‍
    }‌‍‌‍
    for(int i = 1;‌‍ i <= 25;‌‍ i ++) {‌‍‌‍
        shuffle(p + 1, p + n + 1, rnd);‌‍
        suma = sumb = 0;‌‍
        for(int i = 1;‌‍ i <= (n >> 1);‌‍ i ++) {‌‍‌‍
            is_in[p[i]] = false;‌‍
            suma += a[p[i]];‌‍
        }‌‍‌‍
        for(int i = (n >> 1) + 1;‌‍ i <= n;‌‍ i ++) {‌‍‌‍
            is_in[p[i]] = true;‌‍
            sumb += b[p[i]];‌‍
        }‌‍‌‍
        ans = suma * sumb;‌‍
        local_search();‌‍
        res = max(res, ans);‌‍
    }‌‍‌‍
    print(res);‌‍
    putchar('\n');‌‍
}‌‍‌‍
int main()
{‌‍‌‍
    read(T, n, A, B);‌‍
    while(T --) solve();‌‍
    return 0;‌‍
}‌‍‌‍

:::