AGC018C Coins

题意

n=x+y+z 个人,每个人有 xi 个金币,yi 个银币,zi 个铜币,你需要选择 x 个人获得其金币,y 个人获得其银币,z 个人获得其铜币,求获得币数量的最大值。

n105

分析

不妨先钦定所有人都选金币,然后令 ai=yixi,bi=zixi 分别表示将这个人变成银币、铜币的贡献。

考虑发掘一下什么时候选银币比选铜币优。假设有 i,jij 铜的贡献比 ij 银的贡献大,有式子 ai+bj>bi+aj,移项得 aibi>ajbj,由此可得将所有人按 aibi 降序排序后,选银币的人一定在选铜币的人前面,考虑枚举分界线,使得分界线之前选银币,分界线之后选铜币,可以用小根堆预处理出前缀中前 y 大的数之和以及后缀前 z 大的数之和。时间复杂度 O(nlogn)

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof x)
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) ((x)&-(x))
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
//#define double long double
#define int long long
//#define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
template<typename T1,typename T2>inline void ckmx(T1 &x,T2 y){x=x>y?x:y;}
template<typename T1,typename T2>inline void ckmn(T1 &x,T2 y){x=x<y?x:y;}
inline auto rd(){
	int qwqx=0,qwqf=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')qwqf=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){qwqx=(qwqx<<1)+(qwqx<<3)+ch-48;ch=getchar();}return qwqx*qwqf;
}
template<typename T>inline void write(T qwqx,char ch='\n'){
	if(qwqx<0){qwqx=-qwqx;putchar('-');}
	int qwqy=0;char qwqz[40];
	while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
	while(qwqy--)putchar(qwqz[qwqy]);if(ch)putchar(ch);
}
bool Mbg;
const int maxn=1e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,n1,n2,n3;
struct o{
	int x,y,z;
	int a,b;
	bool operator<(const o &p)const
	{
		if(a-b!=p.a-p.b)return a-b>p.a-p.b;
		if(a^p.a)return a>p.a;
		return b<p.b;
	} 
}a[maxn];
int pre[maxn],sec[maxn];
priority_queue<int,vector<int>,greater<int> >q;
inline void solve_the_problem(){
	n1=rd(),n2=rd(),n3=rd(),n=n1+n2+n3;
	int sum=0;
	rep(i,1,n)a[i].x=rd(),a[i].y=rd(),a[i].z=rd(),sum+=a[i].x;
	rep(i,1,n)a[i].a=a[i].y-a[i].x,a[i].b=a[i].z-a[i].x;
	sort(a+1,a+n+1);
//	rep(i,1,n)printf("%lld %lld %lld\n",a[i].x,a[i].y,a[i].z);
//	rep(i,1,n)write(a[i].a,32),write(a[i].b);
	rep(i,1,n2)q.push(a[i].a),pre[n2]+=a[i].a;
	rep(i,n2+1,n){
		q.push(a[i].a);
		pre[i]=pre[i-1]+a[i].a-q.top();
		q.pop();
	}
//	printf("pre: ");rep(i,n2,n)write(pre[i],32);P__;
	while(!q.empty())q.pop();
	rep(i,n-n3+1,n)q.push(a[i].b),sec[n-n3+1]+=a[i].b;
	per(i,n-n3,1){
		q.push(a[i].b);
		sec[i]=sec[i+1]+a[i].b-q.top();
		q.pop();
	}
//	printf("sec: ");rep(i,1,n-n3+1)write(sec[i],32);P__;
	int ans=-llinf;
	rep(i,n2,n-n3)ans=max(ans,pre[i]+sec[i+1]);
	write(ans+sum);
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=1;
	while(_--)solve_the_problem();
}
/*

*/

作者:dcytrl

出处:https://www.cnblogs.com/dcytrl/p/18593511

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   dcytrl  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示