2026/11/15反悔贪心

· · 个人记录

例1:\textcolor{yellow}{P14361 [CSP-S 2025] 社团招新}

题目传送门

最烦人的条件:不能有部门超过\frac{n}{2}个人\ 如果去掉,就是纯贪心\ trick:大于\frac{n}{2}的集合,最多只有一个\ 考虑部门成员换部门后对答案的贡献:secval-maxval\ 维护一个大根堆:(priority_queue默认)

例2: \textcolor{yellow}{P2949 [USACO09OPEN] Work Scheduling G}(课上AC)

#include<bits/stdc++.h>
using namespace std;
#define int long long
/*====================*/
#define endl '\n'
/*====================*/
/*====================*/
const int INF=0x3F3F3F3F3F3F3F3F;
const int N=1e5+10;
/*====================*/
struct node{
    int d,p;
};
bool cmp(node x,node y){
    if(x.d!=y.d)return x.d<y.d;
    else return x.p>y.p;
}
/*====================*/
void Solve(void){
    int n;cin >> n;
    vector<node>a(n+10);
    for(int i=1;i<=n;i++)cin >> a[i].d >> a[i].p;
    sort(a.begin(),a.begin()+n+1,cmp);
    priority_queue<int,vector<int>,greater<int>>pq;
    int ans=0;
    for(int i=1;i<=n;i++){
        if(pq.size()<a[i].d){
            pq.push(a[i].p);
            ans+=a[i].p;
        }else{
            auto x=pq.top();
            if(a[i].p>x){
                pq.pop();pq.push(a[i].p);
                ans-=x;ans+=a[i].p;
            }
        }
    }
    cout << ans;
}
/*====================*/
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1; //cin >> T;
    while (T--)Solve();
    return 0;
}
/*
in:
10
2 99
5 29
4 82
8 72
3 86
8 24
3 69
7 44
3 79
2 19
ans:
515

*/