您的位置:首页 > 其它

HDU3045 Picnic Cows(斜率DP)

2018-01-26 12:01 211 查看
[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

7 3
8 5 6 2 1 7 6

 

[align=left]Sample Output[/align]

8

 

[align=left]Source[/align]
2009 Multi-University Training Contest 14 - Host by ZJNU

题意:有多组数据,N表示序列有N个数,T表示每个子序列最少要有T个数,每个子序列的权值为子序列中所有数减去最小数的和,最后输出所有子序列的权值和。

题解:首先写出dp方程:dp[i]=dp[j]+sum[i]-sum[j]+a[j+1]*(i-j),因为N最大达到了4e5,如果不对dp进行优化会T,所以要对其进行斜率优化。

#include <stdio.h>

#include <algorithm>

#include <iostream>

#define MAXN 400000+10

using namespace std;

int n, t;

int head, tail;

int a[MAXN],que[MAXN];

long long dp[MAXN], sum[MAXN];

int DPup(int j,int k) {

    return dp[j] - sum[j] + j*a[j + 1] - (dp[k] - sum[k] + k*a[k + 1]);

}

int DPdown(int j,int k) {

    return a[j + 1] - a[k + 1];

}

int DPget(int i) {

    return dp[i] = dp[que[head]] + sum[i] - sum[que[head]] - (i - que[head])*a[que[head] + 1];

}

long long  DP() {

    head = tail = 0;

    que[tail++] = 0;

    for (int i = 1; i <= n; i++) {

        while (head + 1 < tail) {

            if(DPup(que[head + 1], que[head]) <= DPdown(que[head + 1], que[head])*i)

                head++;    

        }

        DPget(i);

        if (i >= 2 * t - 1) {//因为题目要求每个子集必须大于等于t,所以要加一个判断

            int j = i - t + 1;

            while (head + 1 < tail) {

                if (DPup(j, que[tail - 1])*DPdown(que[tail - 1], que[tail - 2]) <= DPup(que[tail - 1], que[tail - 2])*DPdown(j, que[tail - 1]))

                    que[tail++] = j;

            }

        }

    }

    return dp
;

}

int main() {

    int _;

    

    while (scanf("%d", &_) != EOF) {

        scanf("%d%d", &n, &t);

        for (int i = 1; i <= n; i++) {

            scanf("%d", &a[i]);

        }

        sum[0] = 0;

        dp[0] = 0;

        sort(a + 1, a + 1 + n);

        for (int i = 1; i <= n; i++) {

            sum[i] = sum[i - 1] + a[i];

        }

        printf("%lld\n", DP());

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  DP 斜率优化