hdu 3045 Picnic Cows(斜率优化dp)

来源:互联网 发布:js同步和异步的理解 编辑:程序博客网 时间:2024/06/05 05:53

题目链接

Picnic Cows

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


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 38 5 6 2 1 7 6
 

Sample Output
8
 

Source
2009 Multi-University Training Contest 14 - Host by ZJNU



这也是用斜率优化做的dp,首先 由贪心知道可以先sort 从小到大排序。用 dp[i]代表前 i 个数分隔得到的最小值。
那么有状态转移方程  dp[i]= min{dp[j]+(sum[i]-sum[j])-a[j+1](i-j)}    k<=j<i-t+1.
之后就是斜率优化了。

这个斜率优化与hdu3507不同,多了一个限制条件,就是每组至少有t个,通过这题我加深了对斜率优化dp的理解,每次在更新栈中元素时,都是判断那些可能成为“分割点”的点,因此在这一题中对元素i此时应该判断的是点i-t+1,并更新。

详见代码:


#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxn=400000+10;typedef long long ll;ll d[maxn],a[maxn],sum[maxn];int q[maxn];int n,t;ll getup(int k,int j){return d[j]-d[k]-sum[j]+sum[k]+j*a[j+1]-k*a[k+1];}ll getdown(int k,int j){return a[j+1]-a[k+1];}void getdp(int j,int i){d[i]=d[j]+sum[i]-sum[j]-(i-j)*a[j+1];}int main(){while(~scanf("%d%d",&n,&t)){for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);sort(a+1,a+n+1);d[1]=0,sum[0]=0;for(int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i];int head=0,tail=0;q[tail++]=0;for(int i=1;i<=n;i++){while(head+1<tail&&getup(q[head],q[head+1])<i*getdown(q[head],q[head+1]))head++;getdp(q[head],i);int j=i-t+1;if(j<t)continue;         //注意此处,此条件是保证第一组元素至少有t个 while(head+1<tail&&getup(q[tail-2],q[tail-1])*getdown(q[tail-1],j)>=getup(q[tail-1],j)*getdown(q[tail-2],q[tail-1]))tail--;q[tail++]=j;}printf("%I64d\n",d[n]);}}










0 0
原创粉丝点击