Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
题意:
n头牛分组,每组至少t头,分组的花费是每头牛的价值与该组最低价值差值的和,求最小花费
分析:
依然是只要求最终结果而不要求分组方式,所以DP;
显然可知,可以打乱顺序,那么久先排序再按顺序分组,这样一定可以保证有最优解,写出朴素dp方程:
表示的是[j,i]分为一组,dp[i]从dp[j-1]转移而来,且j<i+t
设k<j<i+t,且j~i优于k~i,那么:
移项,合并同类项,令:
那么上述不等式可以化为:
斜率优化维护下凸包
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=5e5+7;
int q[maxn];
ll dp[maxn],sum[maxn],a[maxn];
//dp[i]=min{sum[i]-sum[j-1]-(i-j+1)*a[j]+dp[j-1]};
int head,tail,n,t;
ll getdp(int i,int j) {return dp[j-1]+sum[i]-sum[j-1]-(i-j+1)*a[j];}
ll getup(int j,int k) {return ((j-1)*a[j]+dp[j-1]-sum[j-1])-((k-1)*a[k]+dp[k-1]-sum[k-1]);}
ll getdown(int j,int k) {return a[j]-a[k];}
bool check(int i,int j,int k) {return getup(i,j)*getdown(j,k)<=getup(j,k)*getdown(i,j);}
int main()
{
while (~scanf("%d%d",&n,&t))
{
for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
sort(a+1,a+n+1);
sum[0]=0;
for (int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
head=1;tail=0;
for (int i=t;i<2*t;i++) dp[i]=sum[i]-i*a[1];
for (int i=2*t;i<=n;i++)
{
while (head<tail && check(i-t+1,q[tail],q[tail-1])) tail--;
q[++tail]=i-t+1;
while (head<tail && getup(q[head+1],q[head])<=i*getdown(q[head+1],q[head])) head++;
dp[i]=getdp(i,q[head]);
}
printf("%lld\n",dp[n]);
}
return 0;
}