用四个优先队列,分别表示时尚值从小到大/从大到小,美丽值从小到大/从大到小。
每次直接取最大/小的,并标记为删除。取之前判一下是否被删除。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e5+5;
using pii=pair<int,int>;
int n,f[N<<1],b[N<<1],q;
bool d[N<<1];
priority_queue<pii>fx,bx;
priority_queue<pii,vector<pii>,greater<pii>>fn,bn;
int a,c,e,g;
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n*2;i++)cin>>f[i],fx.push({f[i],i}),fn.push({f[i],i});
for(int i=1;i<=n*2;i++)cin>>b[i],bx.push({b[i],i}),bn.push({b[i],i});
while(n--){
cin>>q;
if(q==1){
while(d[fx.top().second])fx.pop();
a+=f[fx.top().second],c+=b[fx.top().second],d[fx.top().second]=1;fx.pop();
while(d[fn.top().second])fn.pop();
e+=f[fn.top().second],g+=b[fn.top().second],d[fn.top().second]=1;fn.pop();
}
else{
while(d[bx.top().second])bx.pop();
a+=f[bx.top().second],c+=b[bx.top().second],d[bx.top().second]=1;bx.pop();
while(d[bn.top().second])bn.pop();
e+=f[bn.top().second],g+=b[bn.top().second],d[bn.top().second]=1;bn.pop();
}
}
cout<<a<<' '<<c<<'\n'<<e<<' '<<g;
return 0;
}