| 比赛 | 
    2024暑假C班集训7 | 
    评测结果 | 
    WWWWTTTTTT | 
    | 题目名称 | 
    买卖 | 
    最终得分 | 
    0 | 
    | 用户昵称 | 
    黄天乐 | 
    运行时间 | 
    12.043 s  | 
    | 代码语言 | 
    C++ | 
    内存使用 | 
    4.21 MiB  | 
    | 提交时间 | 
    2024-07-07 11:35:15 | 
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5;
int n;
struct buy{
	int a;
	int t1;
}buy[MAXN];
struct se{
	int b;
	int t2;
}sell[MAXN];
int cmp(se a,se b){
	if(a.b==b.b){
		return a.t2<b.t2;
	}else return a.b>b.b; 
}
bool vis[MAXN];
int main(){
	freopen("buy.in","r",stdin);
	freopen("buy.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>buy[i].a;
		buy[i].t1=i;
	}
	for(int i=1;i<=n;i++){
		cin>>sell[i].b;
		sell[i].t2=i;
	}
	sort(sell+1,sell+n+1,cmp);
	int ans=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(buy[i].a<sell[j].b&&!vis[j]&&sell[j].t2>=i){
				vis[j]=true;
				ans+=sell[j].b-buy[i].a;
				break;
			}
		}
	} 
	cout<<ans<<endl;
	return 0;
}