HDU 3045 DP斜率优化 解题报告

Picnic Cows

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 (1<=N<=400000)个正数的序列,要求把序列分成若干组,每组的元素个数不能小于T (1 < T <= N)。每一组的代价是每个元素与最小元素的差之和,总代价是每个组的代价之和,求总代价的最小值。
思路:
先对n个数进行排序,则可以分析出分组成员一定是连续的
dp[i]表示前i个数得到的最少值
则:从j~i作为一组
dp[i]=dp[j-1]+sum[i]-sum[j-1]-(i-j+1)*s[j];//sum[i]表示前i个数的和
=>dp[i]=dp[j-1]+sum[i]-sum[j-1]+(j-1)* s[j]-i*s[j];
由于有i*s[j]这一项,所以无法直接在扫描数组的过程中用单调队列维护:
dp[j-1]-sum[j-1]+(j-1)* s[j]-i*s[j]的最小值。
考虑用斜率dp!
假定k < j<=i-t以j~i作为一组比以k~i作为一组更优
则:
dp[j-1]+sum[i]-sum[j-1]-(i-j+1)*s[j] <= dp[k-1]+sum[i]-sum[k-1]-(i-k+1)*s[k]
=>dp[j-1]+sum[i]-sum[j-1]+(j-1)*s[j]-i*s[j] <= dp[k-1]+sum[i]-sum[k-1]+(k-1)*s[k]-i*s[k]
=>(dp[j-1]-sum[j-1]+(j-1)*s[j] - (dp[k-1]-sum[k-1]+(k-1)*s[k]))/(s[j]-s[k])<=i;//保证s[j]>=s[k]
令:
y1 = dp[j-1]-sum[j-1]+(j-1)*s[j]
y2 = dp[k-1]-sum[k-1]+(k-1)*s[k]
x1 = s[j]
x2 = s[k]
所以变成了:
(y1 - y2)/(x1 - x2) <= i;
只需要维护这个斜率即可

代码如下:

#include<cstdio>   
#include<cstring>  
#include<algorithm>   
using namespace std;
#define INF 0x7fffffff
#define N 400010  

int n,t,q[N];  
long long s[N],sum[N],dp[N];  

long long GetY(int j,int k)
{  
    return (dp[j-1]-sum[j-1]+(j-1)*s[j])-(dp[k-1]-sum[k-1]+(k-1)*s[k]);  
}   
long long GetX(int j,int k)
{  
    return s[j]-s[k];  
}  
long long DP(int n)
{  
    int head=0,tail=1;  
    q[0]=1;  
    for(int i=1;i<=n;++i) sum[i]=sum[i-1]+s[i];  
    for(int i=t;i<2*t;++i) dp[i]=sum[i]-i*s[1];//初始化   
    for(int i=2*t;i<=n;++i)
    {//i2*t开始   
        while(head+1<tail&&GetY(i-t+1,q[tail-1])*GetX(q[tail-1],q[tail-2])<=GetY(q[tail-1],q[tail-2])*GetX(i-t+1,q[tail-1])) --tail;  
        q[tail++]=i-t+1;  
        while(head+1<tail && GetY(q[head+1],q[head])<=GetX(q[head+1],q[head])*i) ++head;     
        dp[i]=dp[q[head]-1]+sum[i]-sum[q[head]-1]+(q[head]-1)*s[q[head]]-i*s[q[head]];  
    }  
    return dp[n];  
}  
int main()
{  
    while(~scanf("%d%d",&n,&t))
    {  
        for(int i=1;i<=n;++i) scanf("%I64d",s+i);
        sort(s+1,s+1+n);  
        printf("%I64d\n",DP(n));  
    }  
    return 0;  
}  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值