BZOJ 4368: [IOI2015]boxes纪念品盒

三种路径,左边出去左边回来,右边出去右边回来,绕一圈

绕一圈的路径最多出现一次

那么绕一圈的路径覆盖的点一定是左边半圈的右边和右边半圈的左边

枚举绕一圈的路径的起始点(一定要枚举,这一步不能贪心),更新答案

#include<cstdio>
#include<algorithm>
using namespace std;
int a[10000005],X[10000005],Y[10000005];
long long L[10000005],R[10000005];
bool cmp(int a,int b){
	return a>b;
}
int main(){
	int n,K,l;
	scanf("%d%d%d",&n,&K,&l);
	for (int i=1; i<=n; i++) scanf("%d",&a[i]);
	int Len1=0,Len2=0;
	for (int i=1; i<=n; i++){
		if (!a[i]) continue;
		if (a[i]<=l-a[i]) X[++Len1]=a[i];
		else Y[++Len2]=a[i];
	}
	for (int i=1; i<=Len2/2; i++) swap(Y[i],Y[Len2-i+1]);
	for (int i=1; i<=Len1; i++)
		if (i<=K) L[i]=X[i]*2;
		else L[i]=L[i-K]+X[i]*2;
	for (int i=1; i<=Len2; i++)
		if (i<=K) R[i]=(l-Y[i])*2;
		else R[i]=R[i-K]+(l-Y[i])*2;
	long long ans=L[Len1]+R[Len2];
	for (int i=0; i<K; i++)
		ans=min(ans,L[Len1-i]+R[max(Len2-(K-i),0)]+l);
	printf("%lld
",ans);
	return 0;
}

  

原文地址:https://www.cnblogs.com/silenty/p/9849576.html