1. 程式人生 > >HDU - 3045 - Picnic Cows [ 斜率優化dp ]

HDU - 3045 - Picnic Cows [ 斜率優化dp ]

Problem Describe

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

題意 : 給出N(N<=400000)個數組成的序列,要求劃分成若干組,使得每組至少有T個元素,每組中所有數要減去每組的最小值,問減去的數之和最小為多少。

思路 : 很容易想到轉移方程 dp[ i ] = min { dp[ j ] + ( sum[ j ] - sum[ i ] ) - ( i - j ) * v[ j + 1 ] }
但是題目資料是4e5而這個演算法在時間上過不去,因此我們採用斜率優化的辦法將O(n^2)的演算法降低到O(n)

我們假設在計算dp[ i ] 的時候對於 k 位置有一個更優的位置 j 那麼必然存在
dp[j] + ( sum[i] - sum[j] ) - ( i - j ) * v[j+1] <= dp[k] + ( sum[i] - sum[k] ) - (i - k) * v[k+1]
移項得 (dp[j] - sum[j] + j * v[j+1] ) - (dp[k] - sum[k] + k * v[k+1]) < (v[j+1] - v[k+1] ) * i
然後我們用佇列去優化就行了

AC code :

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

const int maxn = 4e5+50;

ll dp[maxn] ,sum[maxn] ,v[maxn] ;
int que[maxn] ,head ,tail ;
int n ,m ;

ll getdp(int i,int j) {
	return dp[j] + (sum[i] - sum[j]) - (i-j) * v[j+1];
}

ll getdown(int j,int k) {
	return v[j+1] -v[k+1];
}

ll getup(int j,int k) {
	return (dp[j] - sum[j] + j * v[j+1] ) - (dp[k] - sum[k] + k * v[k+1] );
}

int main() {
	while(~scanf("%d %d",&n, &m )) {
		memset(dp,0,sizeof(dp));
		memset(sum,0,sizeof(sum));
		memset(v,0,sizeof(v));
		for (int i = 1;i<=n;i++) scanf("%lld",&v[i]);
		sort(v + 1 ,v + n + 1 );
		for (int i = 1;i<=n;i++) sum[i] = sum[i-1] + v[i];
		head = tail = 0; que[tail ++] = 0;
		for (int i = 2 ; i <= n ; i ++ ) {
			while(head + 1 < tail && getup(que[head+1] ,que[head] ) <= getdown(que[head+1] ,que[head] ) * i ) head ++;
			dp[i] = getdp(i ,que[head] );
			int j = i - m + 1;
			if ( j < m ) continue;
			while(head + 1 < tail && getup(que[tail-1] ,que[tail-2]) * getdown(j ,que[tail-1] ) >= getup(j ,que[tail-1] ) * getdown(que[tail-1] ,que[tail-2] ) ) tail --;
			que[tail ++] = j;
		}
		printf("%lld\n",dp[n]);
	}
	return 0;
}