POJ3016 K-Monotonic DP-左偏树

题目链接:右转进入题目

题目大意:给定N个整数,将Ai修改成Bi的代价时|Ai-Bi|。现在将这N个整数划分成连续的K段,使得每一段单调递增或者单调递减(注意是严格增减)。

问最小代价和。

题解:很明显是DP。

用dp[n][k]表示答案,那么有dp[n][k]=max{dp[p][k-1]+cost(p+1,n)},1<=p<=n<=N,1<=k<=min(n,K),cost(i,j)表示把[i,j]这段区间改成严格单调的最小代价。

cost数组的计算就是n遍BOI的那个数字序列题,见前面的一篇blog([POI]dispatching),用个左偏树就OK了。

具体见黄学长的论文《左偏树的特点及其应用》

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<climits>
#define MAXN 1010
#define ull long long int
#define INF LLONG_MAX
using namespace std;
int a[MAXN],root[MAXN],sz[MAXN],lst_cnt,node_cnt;
int lef[MAXN],rig[MAXN],n,k,num[MAXN];
ull cost[MAXN][MAXN],dp[MAXN][MAXN];
ull val[MAXN],L[MAXN],R[MAXN],dist[MAXN];
ull abs(ull x)
{
	return x>0?x:-x;
}
int merge_lst(int x,int y)
{
	if(!x||!y) return x+y;
	if(val[x]<val[y]) swap(x,y);
	R[x]=merge_lst(R[x],y);
	if(dist[R[x]]>dist[L[x]]) swap(L[x],R[x]);
	if(R[x]) dist[x]=dist[R[x]]+1;
	else dist[x]=0;
	sz[x]=sz[L[x]]+sz[R[x]]+1;
	return x;
}
void pop(int &x)
{
	x=merge_lst(L[x],R[x]);
	return;
}
int newlst(int v)
{
	node_cnt++;
	val[node_cnt]=v;
	sz[node_cnt]=1;
	L[node_cnt]=R[node_cnt]=0;
	dist[node_cnt]=0;
	return node_cnt;
}
ull top(int x)
{
	return val[x];
}
void calc_cost(int pos)
{
	memset(val,0,sizeof(val));
	memset(L,0,sizeof(L));
	memset(R,0,sizeof(R));
	memset(dist,0,sizeof(dist));
	lst_cnt=node_cnt=0;
	ull ans=0;
	for(int i=pos;i<=n;i++)
	{
		root[++lst_cnt]=newlst(a[i]);
		lef[lst_cnt]=rig[lst_cnt]=i;
		num[lst_cnt]=1;
		while(lst_cnt>1&&top(root[lst_cnt])<top(root[lst_cnt-1]))
		{
			lst_cnt--;
			for(int j=lef[lst_cnt];j<=rig[lst_cnt];j++)
				ans-=abs(top(root[lst_cnt])-a[j]);
			root[lst_cnt]=merge_lst(root[lst_cnt],root[lst_cnt+1]);
			rig[lst_cnt]=rig[lst_cnt+1];
			num[lst_cnt]+=num[lst_cnt+1];
			while(sz[root[lst_cnt]]*2>num[lst_cnt]+1) pop(root[lst_cnt]);
		}
		for(int j=lef[lst_cnt];j<=rig[lst_cnt];j++)
			ans+=abs(top(root[lst_cnt])-a[j]);
		cost[pos][i]=min(cost[pos][i],ans);
//		cout<<"lst_cnt="<<lst_cnt<<" ";
//		cout<<"top(rooot[lst_cnt])="<<top(root[lst_cnt])<<" ";
//		cout<<"ans="<<ans<<endl;
	}
	return;
}
int main()
{
	//n=4;a[1]=0;a[2]=-1;a[3]=-2;a[4]=-3;calc_cost(1);
	while(scanf("%d%d",&n,&k)!=EOF&&(n||k))
	{
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				cost[i][j]=INF;
		for(int i=1;i<=n;i++) a[i]-=i;
		for(int i=1;i<=n;i++) calc_cost(i);
//		for(int i=1;i<=n;i++,cout<<endl)
//			for(int j=1;j<=n;j++)
//				printf("%d ",cost[i][j]);
		for(int i=1;i<=n;i++) a[i]+=i;
		for(int i=1;i<=n;i++) a[i]=-a[i];
		for(int i=1;i<=n;i++) a[i]-=i;
		for(int i=1;i<=n;i++) calc_cost(i);
//		for(int i=1;i<=n;i++,cout<<endl)
//			for(int j=1;j<=n;j++)
//				printf("%d ",cost[i][j]);
		for(int i=1;i<=n;i++)
		{
			dp[i][1]=cost[1][i];
			for(int j=2;j<=min(i,k);j++)
			{
				dp[i][j]=INF;
				for(int p=1;p<i;p++)
					dp[i][j]=min(dp[i][j],dp[p][j-1]+cost[p+1][i]);
			}
		}
		printf("%lld\n",dp[n][k]);
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值