【HDU3045】【斜率优化DP】Picnic Cows题解

来源:互联网 发布:生意软件 编辑:程序博客网 时间:2024/06/01 09:19

Picnic Cows

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

#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#define clr(a) memset(a, 0, sizeof(a))#define LL long longusing namespace std;const int maxn = 400010;int head,tail,n,m,q[maxn];LL s[maxn],dp[maxn];inline LL x(int u, int v) { return s[u+1]-s[u]-s[v+1]+s[v]; }inline LL y(int u, int v) { return dp[u]-s[u]+u*(s[u+1]-s[u])-(dp[v]-s[v]+v*(s[v+1]-s[v])); }inline bool check1(int i) { return y(q[head+1], q[head]) <= i*x(q[head+1], q[head]); }inline bool check2(int i) { return y(i, q[tail])*x(q[tail], q[tail-1]) <= y(q[tail], q[tail-1])*x(i, q[tail]); }int main()  {    register int i, j;    while(scanf("%d%d",&n,&m) != EOF) {        head = tail = 0;        q[tail] = 0;        for(i = 1; i <= n; i++) scanf("%I64d",&s[i]);        sort(s+1, s+1+n);        for(i = 2; i <= n; i++) s[i] += s[i-1];        for(i = 1; i <= n; i++) {            while(head < tail && check1(i)) head++;            dp[i] = dp[q[head]]+(s[i]-s[q[head]])-(s[q[head]+1]-s[q[head]])*(i-q[head]);            j = i-m+1;            if(j < m) continue;            while(head < tail && check2(j)) tail--;            q[++tail] = j;        }        printf("%I64d\n", dp[n]);    }}
原创粉丝点击