跳到正文

题解

E. Red and Green Apples

gyh20约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年6月14日
更新
2026年6月14日
篇幅
约 2 分钟

版本

  1. v1当前

贪心。每一次肯定是用 CC 中最大的去顶替 A/BA/B 中的最小的,数组排序即可。

具体来说,将 AA,BB,CC,分别排序,然后用三个指针分别指向 AA,BB,CC 分别选到哪个地方了,比较 AA,BB 的最小值是否小于 CC 即可。

C++
#include<bits/stdc++.h>
#define re register
inline int read(){
	re int t=0;
	re char v=getchar();
	while(v<'0')v=getchar();
	while(v>='0'){
		t=(t<<3)+(t<<1)+v-48;
		v=getchar();
	}
	return t;
}
using namespace std;
int n,x,y,m,k,a[100002],b[100002],c[100002],l;
long long ans;
signed main(){
	x=read();y=read();
	n=read();	m=read();	k=read();for(re int i=1;i<=n;++i)a[i]=read();sort(a+1,a+n+1);x=n-x+1;
for(re int i=1;i<=m;++i)b[i]=read();sort(b+1,b+m+1);y=m-y+1;
for(re int i=1;i<=k;++i)c[i]=read();sort(c+1,c+k+1);
	l=k;b[m+1]=a[n+1]=1e9;
	while(l){
		if(a[x]<b[y]&&x<=n){
			if(c[l]>a[x])ans+=c[l],--l,++x;
			else break;
		}
		else if(y<=m){
			if(c[l]>b[y])ans+=c[l],--l,++y;
			else break;
		}
		else break;
	}
	for(re int i=x;i<=n;++i)ans+=a[i];
	for(re int i=y;i<=m;++i)ans+=b[i];
	printf("%lld",ans);
}
原文