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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值