#6035. 「雅礼集训 2017 Day4」洗衣服

题目

前言

这个贪心有点妙,考试的时候没有想出来,一看题解恍然大悟。

分析

首先对于洗衣服,显而易见我们可以用堆来处理,可以得出每件衣服洗完的时间 \(t_i\),其中 \(t_i\) 表示的是第 \(i\) 早的衣服洗完的时间,那对与烘衣服呢?正着做好像不太好做,因为每件衣服的洗完时间都不同,不能(至少我不会)贪心。那我们可以假定一个洗完烘完衣服的时间,从那个时间点往前做,就是倒着做,然后又可以用堆来做了,得到 \(s_i\) ,含义与 \(t_i\) 类似,即烘衣服花的时间,那怎么匹配呢,显然是最大的和最小的,次大的和次小的 \(\dots\)以此类推。如果不是这样子,显然会使其中一个值变大,使得答案可能会增大,显然不会是最优的。

\(code\)

#include<bits/stdc++.h>
#define N 100005
#define M 1000005
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}
int n,na,nb;
int a[N],b[N];
long long ans,t[M],s[M];
struct node{
	long long ed;
	int use;
	bool operator < (const node &A) const {return A.ed<ed;}
};
priority_queue<node> q1,q2;
signed main(){
//	freopen("laundry.in","r",stdin);
//	freopen("laundry.out","w",stdout);
	n=read(),na=read(),nb=read();
	for(int i=1;i<=na;++i) a[i]=read(),q1.push((node){a[i],a[i]});
	for(int i=1;i<=nb;++i) b[i]=read(),q2.push((node){b[i],b[i]});
	for(int i=1;i<=n;++i){
		t[i]=q1.top().ed,q1.push((node){t[i]+q1.top().use,q1.top().use});
		q1.pop();
		s[i]=q2.top().ed,q2.push((node){s[i]+q2.top().use,q2.top().use});
		q2.pop();
	}
	for(int i=1;i<=n;++i) ans=max(ans,t[i]+s[n-i+1]);
	printf("%lld\n",ans);
//	fclose(stdin);
//	fclose(stdout);
	return 0;
} 
posted @ 2022-12-11 14:47  Aurora-JC  阅读(45)  评论(0编辑  收藏  举报