HDU3045——Picnic Cows(斜率优化DP)

Picnic Cows

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1329    Accepted Submission(s): 396

Problem Description
It’s summer vocation now. After tedious milking, cows are tired and wish to take a holiday. So Farmer Carolina considers having a picnic beside the river. But there is a problem, not all the cows consider it’s a good idea! Some cows like to swim in West Lake, some prefer to have a dinner in Shangri-la ,and others want to do something different. But in order to manage expediently, Carolina coerces all cows to have a picnic!
Farmer Carolina takes her N (1<N≤400000) cows to the destination, but she finds every cow’s degree of interest in this activity is so different that they all loss their interests. So she has to group them to different teams to make sure that every cow can go to a satisfied team. Considering about the security, she demands that there must be no less than T(1<T≤N)cows in every team. As every cow has its own interest degree of this picnic, we measure this interest degree’s unit as “Moo~”. Cows in the same team should reduce their Moo~ to the one who has the lowest Moo~ in this team——It’s not a democratical action! So Carolina wishes to minimize the TOTAL reduced Moo~s and groups N cows into several teams.
For example, Carolina has 7 cows to picnic and their Moo~ are ‘8 5 6 2 1 7 6’ and at least 3 cows in every team. So the best solution is that cow No.2,4,5 in a team (reduce (2-1)+(5-1) Moo~)and cow No.1,3,6,7 in a team (reduce ((7-6)+(8-6)) Moo~),the answer is 8.
 
Input
The input contains multiple cases.
For each test case, the first line has two integer N, T indicates the number of cows and amount of Safe-base line.
Following n numbers, describe the Moo~ of N cows , 1st is cow 1 , 2nd is cow 2, and so on.
 
Output
One line for each test case, containing one integer means the minimum of the TOTAL reduced Moo~s to group N cows to several teams.
 
Sample Input
7 3
8 5 6 2 1 7 6
 
Sample Output
8
 
Source
2009 Multi-University Training Contest 14 - Host by ZJNU
 
Recommend
gaojie

解析:

        题目大意是要把n个数分为若干组,每组至少t个数,让所有组里的每一个数与该组最小数的差的总和尽量小。。。

        很容易想到朴素的n^2的dp方程 dp[i]=min{dp[j]+sum[i]-sum[j]+(i-j)*a[j+1]}

        当然上面的方程是从小到大排序后的,sum[i]表示前缀和。。。

        要求分段的序列长度不小于 m,即 i- j+ 1>= m  假设 j> k, 要使决策 j 比决策 k 优, 则有 dp[j]+ sum[i]- sum[j]+ ( i- j )* data[j+ 1]< dp[k]+ sum[i]- sum[k]+ ( i- k )* data[k+ 1]

        整理有: dp[j]- sum[j]- j* data[j+ 1]- ( dp[k]- sum[k]- k* data[k+ 1] ) < i* ( data[j+ 1]- data[k+ 1] )  

        所以 j 比 k 优等价于 ( dp[j]- dp[k]- sum[j]+ sum[k]- j* data[j+ 1]+ k* data[k+ 1] )/ ( data[j+ 1]- data[k+ 1] )< i  今 F[j,k]= 上式左边 满足F[j,k]<i,可以剔除k状态------------------------      

(1) 对于 k< j< i< t 假设 F[j, k]> F[i, j] 如果 F[i, j]< t 则 i 比 j 优 如果 F[i, j]> t 则 F[j, k]> t 得到 k 比 j 优 故对于满足条件 F[j,k]> F[i,j] 的可以剔除 j 状态---

(2)  因为长度要不小于 2* m 故 1到 m- 1 都不可能成为决策点 对于 i, 能成为 i 状态的决策点的最小 j 为 i- m+ 1,并且对于 t> i, 这个最小 j= i- m+ 1 也有可能成为 t 的决策点 故对于当前状态 i ( i- m+ 1>= m ) 将决策点 i- m+ 1 加入 

代码:

#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;

int n,d;
long long a[50005];
long long sum[50005];
long long dp[50005];
int q[50005];

long long S(int j,int k)
{
	return a[j+1]-a[k+1];
}

long long G(int j,int k)
{
	return dp[j]-sum[j]+j*a[j+1]-(dp[k]-sum[k]+k*a[k+1]);
}



void work()
{
	memset(dp,0,sizeof(dp));
	int st=0,ed=0;
	q[++ed]=0;
	for(int i=d;i<=n;i++)
	{
		while(st<ed && G(q[st+1],q[st])<=i*S(q[st+1],q[st]))st++;
		dp[i]=dp[q[st]]+sum[i]-sum[q[st]]-(i-q[st])*a[q[st]+1];
		if(i-d+1>=d)q[++ed]=i-d+1;
		int j=q[ed--];
        while( st< ed && G( q[ed], q[ed-1] )* S( j, q[ed] )>=G( j, q[ed] )* S( q[ed], q[ed-1] ) ) ed--;
		q[++ed]=j;
    }
        printf("%I64d\n", dp[n] );
}


void read()
{
	freopen("hdu3045.in","r",stdin);
	freopen("hdu3045.out","w",stdout);
	while(scanf("%d%d",&n,&d)!=EOF)
	{
	   sum[0]=0;
	   for(int i=1;i<=n;i++)
	   {
	       scanf("%I64d",&a[i]);
		   sum[i]=sum[i-1]+a[i];
	   }
	   sort(a+1,a+1+n);
	   work();
    }
}

int main()
{
	read();
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值