题解:CF798D Mike and distribution

· · 题解

我们需要同时考虑两个序列的贡献,直接同时处理两个维度比较复杂,可以先对其中一个序列进行排序处理,然后在此基础上处理另一个序列。

我们可以将元素按照 A 值从大到小排序,然后对剩下的元素两两一组,每组选择 B 值较大的那个(保证 B 的和足够大)。

代码实现

// user : 1008686
// name : wayneoi
// problem : CF798D Mike and distribution
// time : 2025/08/17
// target : 100 pts

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct node {
    int a, b, id;
}t[N];
int n;
inline bool cmp(node a, node b){
    return a.a > b.a;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> t[i].a;
    for(int i = 1; i <= n; i++){
        cin >> t[i].b;
        t[i].id = i;
    }
    sort(t + 1, t + 1 + n, cmp);
    // for(int i = 1; i <= n; i++) cout << t[i].id << " ";
    cout << (n >> 1) + 1 << "\n" << t[1].id << " ";
    for(int i = 2; i <= n; i += 2){
        if(t[i].b >= t[i + 1].b || i == n) cout << t[i].id << " ";
        else cout << t[i + 1].id << " ";
    }
    return 0;
}