0%

ABC-376-E - Max × Sum

题目大意

给定多组数据。每组有 nn 个物品,每个物品有两个值 AiA_iBiB_i,需要选出恰好 kk 个物品,使得 max(A)\max(A) 与所选 BB 的和构成的目标值最小(题目要求输出该最小值)。

AC代码

总体思路就是A的值只由最大Ar决定,那么其他值怎么填那?自然是使他们的B值总和最小

然后记得sumB要归0。

AC https://atcoder.jp/contests/abc376/submissions/59002823

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll N=2e5+10;
ll n,k,T,ans,B[N],sumB;
pair<ll,ll> A[N];
// struct comp {
// bool operator()(ll a,ll b) {
// if(B[a]!=B[b]) {
// return B[a]<B[b];
// }
// return false;
// }
// };
priority_queue<ll> pq;
bool cmp(pair<ll,ll> a,pair<ll,ll> b) {
if(a.first!=b.first)
return a.first<b.first;
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
for(int _=1;_<=T;++_) {
cin>>n>>k;
priority_queue<ll> empty;
swap(pq,empty); // 模拟clear操作
ans=1e18+7;sumB=0;
for(int i=1;i<=n;++i) {
ll ae;
cin>>ae;
A[i]=make_pair(ae,i);
}
for(int i=1;i<=n;++i) {
cin>>B[i];
}
sort(&A[1],&A[n+1],cmp);
for(int i=1;i<=k;++i) {
sumB+=B[A[i].second];
pq.push(B[A[i].second]);
}
ans=sumB*A[k].first;
for(int i=k+1;i<=n;++i) {
sumB-=pq.top();
sumB+=B[A[i].second];
pq.pop();
pq.push(B[A[i].second]);
ans=min(ans,sumB*A[i].first);
}
cout<<ans<<endl;
}
return 0;
}
// AC https://atcoder.jp/contests/abc376/submissions/59002823

心路历程(WA,TLE,MLE……)