Olympiad in Programming and Sports 题解
plank_black · · 题解
  看到各路大佬都用了费用流等高级的算法,奈何蒟蒻技术力不够,在此仅讲解一种反悔贪心的思路。
  首先考虑一个普通的贪心思路,先在这群人中选出编程能力最强的前
  明显上面的做法有局限性,反例很好构造,所以我们来考虑怎么反悔。
  前半段同样的选出编程能力最强的前
-
选择没有被选过的人(编号为
i ),贡献为B_i 。 -
选择在编程团队里的人(编号为
i ),再在没选过的人里选取编程能力最强的人补位(编号为j ),贡献为B_i - A_i + A_j 。
  观察上边两个算式我们发现,我们只需要维护三个大根堆,即
#include<algorithm>
#include<iostream>
#include<climits>
#include<queue>
using namespace std;
const int N = 3e3 + 10;
int n, p, s;
int arr[N];
struct node {
int id, val;
bool operator< (const node a) const {
return val < a.val;
}
}A[N], B[N];
priority_queue<node> q1, q2, q3;
bool cmp1(node a, node b);
bool cmp2(node a, node b);
int main( ) {
cin >> n >> p >> s;
for(int i = 1; i <= n; i++) {
A[i].id = i;
cin >> A[i].val;
}
for(int i = 1; i <= n; i++) {
B[i].id = i;
cin >> B[i].val;
}
sort(A + 1, A + n + 1, cmp1);
int sum = 0;
for(int i = 1; i <= p; i++) {
arr[A[i].id] = 1;
sum += A[i].val;
}
sort(A + 1, A + n + 1, cmp2);
for(int i = 1; i <= n; i++) {
q1.push({i, A[i].val});
q2.push({i, B[i].val});
q3.push({i, B[i].val - A[i].val});
}
for(int i = 1; i <= s; i++) {
int add = INT_MIN, a, b, op;
while(!q2.empty( ) && arr[q2.top( ).id]) q2.pop( );
if(!q2.empty( ))
if(q2.top( ).val > add) {
add = q2.top( ).val;
a = q2.top( ).id;
op = 1;
}
while(!q1.empty( ) && arr[q1.top( ).id]) q1.pop( );
while(!q3.empty( ) && arr[q3.top( ).id] != 1) q3.pop( );
if(!q1.empty( ) && !q3.empty( )) {
int tmp = q1.top( ).val + q3.top( ).val;
if(tmp > add) {
add = tmp;
a = q3.top( ).id;
b = q1.top( ).id;
op = 2;
}
}
sum += now;
if(op == 1) {
q2.pop( );
arr[a] = 2;
} else {
q1.pop( );
q3.pop( );
arr[a] = 2;
arr[b] = 1;
q3.push({b, B[b].val - A[b].val}); //选了一个人到编程团队里就应该多一次反悔的机会
}
}
cout << sum << endl;
for(int i = 1; i <= n; i++)
if(arr[i] == 1) cout << i << ' ';
cout << endl;
for(int i = 1; i <= n; i++)
if(arr[i] == 2) cout << i << ' ';
cout << endl;
return 0;
}
bool cmp1(node a, node b) {
return a.val > b.val;
}
bool cmp2(node a, node b) {
return a.id < b.id;
}