Picnic Cows (hdu 3045)

Picnic Cows

Time Limit : 8000/4000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 11
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(T已知),现在定义一个数组的大小=该数组里的每个元素-该数组里的最小值的和,现在要求各个子数组的大小和最小,求这个最小大小。

思路

斜率DP:斜率优化DP详解

dp[i]:前i个数分隔可以得到的最小值 ;
sum[i]:输入的a[ ]排序后的前缀和 ;

状态转移方程: dp[i] = min( dp[k] + sum[i] - sum[k] - (i-k)*a[k+1]) ;

=> i*a[k+1] + dp[i] - sum[i] = dp[k] - sum[k] +k*a[k+1] ;

设 X: a[k+1] , B: dp[i] - sum[i] , Y: dp[k] - sum[k] +k*a[k+1];

=> K*X=Y (直线方程)

其中斜率K=i ;

斜率求解:K=(Y1-Y2) / (X1-X2) ;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

const int MAX=400000+100;

long long a[MAX];
long long sum[MAX];
long long dp[MAX];
long long q[MAX];

//计算斜率

long long up(int j, int k) // y1-y2
{
    return ((dp[j]-sum[j]+j*a[j+1])-(dp[k]-sum[k]+k*a[k+1]));
}

long long down(int j, int k) //x1-x2
{
    return (a[j+1]-a[k+1]);
}

int main()
{
    int n,t;
    while(scanf("%d%d",&n,&t)!=EOF)
    {
        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]; //sum[]:前缀和
        }
        int Left=1;
        int Right=1;
        q[1]=0;
        dp[0]=0;
        for(int i=1;i<=n;i++)
        {
            while(Left<Right&&(up(q[Left+1],q[Left]))<=i*(down(q[Left+1],q[Left])))
                Left++;
            int Front=q[Left];
            dp[i]=dp[Front]+sum[i]-sum[Front]-(i-Front)*a[Front+1];
            
            //若该子数组元素个数<t , 舍去
           if((i-t+1)<t)
                continue;

            while(Left<Right&&up(q[Right],q[Right-1])*down(i-t+1,q[Right])>=up(i-t+1,q[Right])*down(q[Right],q[Right-1]))
                Right--;
            q[++Right]=i-t+1;
        }
        printf("%lld\n",dp[n]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值