HDU3045 Picnic Cows(斜率DP)

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
 

Source
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[n];
}
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());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值