【BZOJ4868】[六省联考2017]期末考试(贪心)

【BZOJ4868】[六省联考2017]期末考试(贪心)

题面

BZOJ
洛谷

题解

显然最终的答案之和最后一个公布成绩的课程相关。
枚举最后一天的日期,那么维护一下前面有多少天可以向后移,后面总共需要往前移多少天,扫一遍贪心就好了。

#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
#define MAX 100100
inline int read()
{
	int x=0;bool t=false;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')t=true,ch=getchar();
	while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
	return t?-x:x;
}
int A,B,C,n,m,d,tc,t[MAX],b[MAX];
int ed[MAX],ot[MAX];
ll ans,L,R,cl;
int main()
{
	A=read();B=read();C=read();A=min(A,B);
	n=read();m=read();
	for(int i=1;i<=n;++i)t[i]=read();
	for(int i=1;i<=m;++i)d=max(d,b[i]=read()),ot[b[i]]+=1;
	for(int i=1;i<=m;++i)R+=d+1-b[i];
	for(int i=1;i<=n;++i)if(t[i]<=d)ed[t[i]]++,ans+=1ll*(d+1-t[i])*C,cl+=d+1-t[i],++tc;
	for(int i=d,sl=m,sr=0,sc=tc;i;--i)
	{
		R-=sl;L+=sr;sl-=ot[i];sr+=ot[i];cl-=sc;sc-=ed[i];
		ll ret=C*cl;if(R>=L)ret+=A*L;else ret+=A*R+B*(L-R);
		ans=min(ans,ret);
	}
	printf("%lld
",ans);
	return 0;
}
原文地址:https://www.cnblogs.com/cjyyb/p/10597197.html