题解 HDOJ4261 【Estimation】

题目链接:Link

Problem

Solution

显然不难找到状态转移方程,关键是如何快速预处理出某一段区间的代价。
易证对于某个确定的区间,b值就是中位数。在用对顶堆维护时顺带维护当前堆里的元素和即可在 $ O(n^2 log n) $ 内预处理完(15000ms好评)。。。

Code

//http://acm.hdu.edu.cn/showproblem.php?pid=4261
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cassert>
using namespace std;
typedef long long LL;
const int maxn=2005;
const int maxm=30;
int n,m,a[maxn];
LL f[maxm][maxn],b[maxn][maxn];
int main()
{
	#ifdef local
	freopen("pro.in","r",stdin);
	#endif
	while(scanf("%d%d",&n,&m)==2&&!(n==0&&m==0))
	{
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1;i<=n;i++)
		{
			priority_queue<LL> L; priority_queue<LL,vector<LL>,greater<LL> > R;
			L.push(a[i]);
			LL SL=a[i],SR=0;
			for(int j=i+1;j<=n;j++)
			{
				if(a[j]>L.top()) { R.push(a[j]); SR+=a[j]; }
				else { L.push(a[j]); SL+=a[j]; }
				while((int)L.size()-(int)R.size()>=2) { SL-=L.top(); SR+=L.top(); R.push(L.top()); L.pop(); }
				while((int)R.size()-(int)L.size()>=1) { SR-=R.top(); SL+=R.top(); L.push(R.top()); R.pop(); }
				b[i][j]=L.top()*L.size()-SL+SR-L.top()*R.size();
				// printf("zws(%d,%d)=%d
",i,j,L.top());
				// printf("b(%d,%d)=%d
",i,j,b[i][j]);
			}
		}
		memset(f,0x3f,sizeof(f));
		f[0][0]=0;
		for(int i=1;i<=m;i++)
			for(int j=i;j<=n;j++)
				for(int k=i-1;k<j;k++)
					f[i][j]=min(f[i][j],f[i-1][k]+b[k+1][j]);
		printf("%lld
",f[m][n]);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/happyZYM/p/11581076.html