1. CF730I Olympiad in Programming and Sports

大意: $n$个人, 第$i$个人编程能力$a_i$, 运动能力$b_i$, 要选出$p$个组成编程队, $s$个组成运动队, 每个队的收益为队员能力和, 求最大收益.

费用流做法很显然, 开两个点$X,Y$表示编程和运动, 源点向每个人连边, 代价为$0$, 每个人向$X$连边, 代价为编程能力, 每个人向$Y$连边, 代价为运动能力, $X$向汇点连边容量为$p$, $Y$向汇点连边, 容量为$s$, 然后求出最大费用最大流即为答案.

考虑用堆模拟费用流. 每次增广只有四种情况.

  • 添加一个未被选择的人去编程队
  • 添加一个未被选择的人去运动队
  • 让一个人从运动队到编程队, 再选择一个未被选择的人去运动队
  • 让一个人从编程队到运动队, 再选择一个未被选择的人去编程队

前两种情况直接用堆维护最大即可. 后两种情况的话, 在每次决策时, 都把替换的收益扔进一个堆里即可.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 1e6+10;
int n, x, y;
int a[N], b[N], va[N], vb[N];
pii s[5];
priority_queue<pii> q[5];
ll ans;

pii get(priority_queue<pii> &q, int *v1, int *v2) {
    while (q.size()&&(v1[q.top().y]||v2[q.top().y])) q.pop();
    return q.empty()?pii(-INF,0):q.top();
}
void add(int u, int tp) {
    if (va[u]) ans-=a[u],va[u]=0,++x;
    if (vb[u]) ans-=b[u],vb[u]=0,++y;
    if (tp==1) ans+=a[u],va[u]=1,--x,q[4].push(pii(b[u]-a[u],u));
    else ans+=b[u],vb[u]=1,--y,q[3].push(pii(a[u]-b[u],u));
}

int main() {
    scanf("%d%d%d", &n, &x, &y);
    REP(i,1,n) scanf("%d",a+i);
    REP(i,1,n) scanf("%d",b+i);
    REP(i,1,n) { 
        q[1].push(pii(a[i],i));
        q[2].push(pii(b[i],i));
    }
    int tot = x+y;
    REP(i,1,tot) {
        s[1] = get(q[1],va,vb);
        s[2] = get(q[2],va,vb);
        s[3] = get(q[3],va,va);
        s[4] = get(q[4],vb,vb);
        s[3].x += s[2].x;
        s[4].x += s[1].x;
        if (!x) s[1].x=s[3].x=-INF;
        if (!y) s[2].x=s[4].x=-INF;
        int p = max_element(s+1,s+5)-s;
        if (p==1) add(s[1].y,1);
        else if (p==2) add(s[2].y,2);
        else if (p==3) add(s[3].y,1),add(s[2].y,2);
        else add(s[4].y,2),add(s[1].y,1);
    }
    printf("%lld\n", ans);
    REP(i,1,n) if (va[i]) printf("%d ",i);hr;
    REP(i,1,n) if (vb[i]) printf("%d ",i);hr;
}
View Code

相关文章:

  • 2022-01-25
  • 2022-03-09
  • 2022-01-13
  • 2022-12-23
  • 2021-07-06
  • 2021-06-11
猜你喜欢
  • 2022-12-23
  • 2021-12-01
  • 2022-12-23
  • 2021-11-26
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
相关资源
相似解决方案