奶long
P10010 题解
题意
给定由
求得分的最大值。
思路
贪心 + 搜索。
我们有一个很暴力的想法:枚举
我们注意到
但是这种方法有问题:在
514 114
1919 810
888 666
按照上述贪心所得到的“最大值”为
我们将
这样就对了。
证明
该部分使用 Qwen AI 指导。
对贪心部分使用微扰法。
设
用原得分减去新得分,可得
我们现在需要解决这个问题:什么时候能满足
推不等式:
移项得:
两边同时除以
即:
但是这样还是不好排序,所以我们得换一种证明方法:交换两个元素(把
设
那么原得分为:
经过多次尝试,在效率与准确性的平衡下,我们发现在距离中点左右
代码
#include<bits/stdc++.h>
using namespace std;
#define int __int128
#define fir first
#define sec second
int max(int a, int b){
if(a > b) return a;
else return b;
}
int min(int a, int b){
if(a > b) return b;
else return a;
}
int read(){
int x = 0;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9'){
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void write(int x){
if(x == 0) {
putchar('0');
return;
}
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
return;
}
pair<int, int> a[100010];
int T, n, tmp, mid, ansa, ansb, ans;
bool cmp(pair<int, int> x, pair<int, int> y){
return x.fir * y.sec > y.fir * x.sec;
}
void dfs(int i, int r, int suma, int sumb){
if(i > r){
ans = max(ans, suma * sumb);
return;
}
dfs(i + 1, r, suma + a[i].fir, sumb);
dfs(i + 1, r, suma, sumb + a[i].sec);
}
void solve(int pos){
int l = max(pos - 8, 1), r = min(pos + 8, n);
int suma = 0, sumb = 0;
for(int i = 1; i <= l; i++) suma += a[i].fir;
for(int i = r; i <= n; i++) sumb += a[i].sec;
dfs(l + 1, r - 1, suma, sumb);
}
signed main(){
T = read();
n = read();
tmp = read();
tmp = read();
mid = n / 2;
while(T--){
ansa = ansb = ans = 0;
for(int i = 1; i <= n; i++){
a[i].fir = read();
a[i].sec = read();
}
sort(a + 1, a + n + 1, cmp);
int sl = max(mid - 32, 1);
int sr = min(mid + 32, n);
for(int i = sl; i <= sr; i++) solve(i);
write(ans);
putchar('\n');
}
return 0;
}
ヾ(•ω•`)o