题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3045

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只奶牛,每只奶牛都有一个Moo[i]值,代表它对野餐的感兴趣程度;

现在要给奶牛们分成若干组,限定每组至少有t只奶牛;

每一组的奶牛的Moo值都会减少为其所在组的奶牛们中最小的Moo值。

题解:

首先对N只奶牛按照Moo值从小到大排序一下,重新编号为1~N;

设dp[i]代表前i项的minimize the TOTAL reduced Moo~s;

设sum[i]是Moo[]的前缀和数组;

则状态转移方程为dp[i] = min{ dp[j] + ( sum[i] - sum[j] ) - Moo[j+1] * ( i - j ) },t ≤ j ≤ i - t;

可以看出要完成全部状态转移,是O(n2)的时间复杂度,N≤400000的范围,显然是要超时的,上斜率优化。

在计算dp[i]时,对于j的可能取值,假设有a,b,满足t ≤ a < b ≤ i - t,

若有dp[b] + ( sum[i] - sum[b] ) - Moo[b+1] * ( i - b ) ≤ dp[a] + ( sum[i] - sum[a] ) - Moo[a+1] * ( i - a )

则可以说b点优于a点;

对上式进行变形可得:

我们不妨设

那么就有:

b点优于a点 <=> g(a,b) ≤ i

b点劣于a点 <=> g(a,b) > i

于是进一步的,就有:

在计算dp[i]时,对于j的可能取值,假设有a,b,c,满足t ≤ a < b < c ≤ i - t,

若g(a,b) ≥ g(b,c),则b点必然淘汰。

证明:若g(b,c) ≤ i,则c点优于b点;若g(b,c) > i,则g(a,b) ≥ g(b,c) > i,则b点劣于a点;不管如何,b点都被淘汰。

再然后就是按照上面的性质,维护一个下凸的图形,斜率逐渐增大。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=+; int n,t;
LL Moo[maxn],sum[maxn];
LL dp[maxn];
int q[maxn],head,tail; LL up(int a,int b)
{
return (dp[b]-sum[b]+Moo[b+]*b)-(dp[a]-sum[a]+Moo[a+]*a);
}
LL down(int a,int b)
{
return Moo[b+]-Moo[a+];
} int main()
{
while(scanf("%d%d",&n,&t)!=EOF)
{
for(int i=;i<=n;i++) scanf("%I64d",&Moo[i]);
sort(Moo+,Moo+n+);
sum[]=;
for(int i=;i<=n;i++) sum[i]=sum[i-]+Moo[i]; head=tail=;
dp[]=;
for(int i=;i<=min(*t-,n);i++) dp[i]=dp[i-]+Moo[i]-Moo[]; for(int i=*t;i<=n;i++)
{
int j=i-t;
while(head+<tail)
{
int a=q[tail-], b=q[tail-];
if(up(a,b)*down(b,j)>=up(b,j)*down(a,b)) tail--;
else break;
}
q[tail++]=j; while(head+<tail)
{
int a=q[head], b=q[head+];
if(up(a,b)<=i*down(a,b)) head++;
else break;
}
j=q[head];
dp[i]=dp[j]+(sum[i]-sum[j])-Moo[j+]*(i-j);
} printf("%I64d\n",dp[n]);
}
}

注意,本题Moo[i]没有明确说明范围,所以用int就爆掉了,要用long long,并且也不能直接g=up/down然后去比较g(),只能化成乘法形式。

HDU 3045 - Picnic Cows - [斜率DP]的更多相关文章

  1. hdu 3045 Picnic Cows(斜率优化DP)

    题目链接:hdu 3045 Picnic Cows 题意: 有n个奶牛分别有对应的兴趣值,现在对奶牛分组,每组成员不少于t, 在每组中所有的成员兴趣值要减少到一致,问总共最少需要减少的兴趣值是多少. ...

  2. HDU 3045 Picnic Cows(斜率优化DP)

    Picnic Cows Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. HDU 3045 picnic cows(斜率DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3045 题目大意:有n个数,可以把n个数分成若干组,每组不得小于m个数,每组的价值=除了该组最小值以外每 ...

  4. HDU 3045 Picnic Cows

    $dp$,斜率优化. 设$dp[i]$表示$1$至$i$位置的最小费用,则$dp[i]=min(dp[j]+s[i]-s[j]-(i-j)*x[j+1])$,$dp[n]$为答案. 然后斜率优化就可以 ...

  5. HDU3045 Picnic Cows —— 斜率优化DP

    题目链接:https://vjudge.net/problem/HDU-3045 Picnic Cows Time Limit: 8000/4000 MS (Java/Others)    Memor ...

  6. HDU 2829 Lawrence (斜率DP)

    斜率DP 设dp[i][j]表示前i点,炸掉j条边的最小值.j<i dp[i][j]=min{dp[k][j-1]+cost[k+1][i]} 又由得出cost[1][i]=cost[1][k] ...

  7. HDU 3507 - Print Article - [斜率DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3507 Zero has an old printer that doesn't work well s ...

  8. HDU 3480 Division(斜率DP裸题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480 题目大意:将n个数字分成m段,每段价值为(该段最大值-该段最小值)^2,求最小的总价值. 解题思 ...

  9. [kuangbin带你飞]专题二十 斜率DP

            ID Origin Title   20 / 60 Problem A HDU 3507 Print Article   13 / 19 Problem B HDU 2829 Lawr ...

随机推荐

  1. LR 测试http协议xml格式数据接口

    Action() { lr_start_transaction("T1"); web_custom_request("xxxxHTTPRequest", &qu ...

  2. ZTree async中文乱码,ZTree reAsyncChildNodes中文乱码,zTree中文乱码

    ZTree async中文乱码,ZTree reAsyncChildNodes中文乱码,zTree中文乱码 >>>>>>>>>>>&g ...

  3. Selenium 基本用法

    如下,使用 Selenium 打开淘宝首页并获取页面源代码: from selenium import webdriver browser = webdriver.Chrome() # 声明一个浏览器 ...

  4. 数字图像处理笔记与体会(一)——matlab编程基础

    最近开始学习数字图像处理,使用matlab实现,下面我就来记录笔记和体会,一方面是给大家提供参考,另一方面是防止我忘记了. 复习一下: 1.数字图像是用一个数字矩阵来表示的,数字阵列中的每个数字,表示 ...

  5. 在 Ubuntu 13.10 安装 PyCharm 3.0.1 & Oracle JDK

    由于授权问题,在较新的Linux发行版本中都不再包含Oracle Java,取而代之的是OpenJDK.Ubuntu也是如此. OpenJDK能满足大部分的应用程序运行条件,但PyCharm无法在Op ...

  6. 在PowerDesigner中自动生成sqlserver字段备注

    在PowerDesigner中自动生成sqlserver字段备注 PowerDesigner是数据库设计人员常用的设计工具,但其自生默认生成的代码并不会生成sqlserver数据库的字段备注说明.在生 ...

  7. EGit系列第二篇——关联远程仓库

    网上也有很多代码托管网站支持git,像最出名的GitHub,还有国内支持私有项目的OSC开源中国和CSDN等... 首先得注册个帐号,然后才可以创建仓库 一般都会带一个ReadMe.md,你可以勾选也 ...

  8. 【cs229-Lecture11】贝叶斯统计正则化

    本节知识点: 贝叶斯统计及规范化 在线学习 如何使用机器学习算法解决具体问题:设定诊断方法,迅速发现问题 贝叶斯统计及规范化(防止过拟合的方法) 就是要找更好的估计方法来减少过度拟合情况的发生. 回顾 ...

  9. Qt编写视频播放器(vlc内核)

    在研究qt+vlc的过程中,就想直接做个播放器用于独立的项目,vlc还支持硬件加速,不过部分电脑硬件不支持除外.用vlc的内核写播放器就是快,直接调用api就行,逻辑处理和ui展示基本上分分钟的事情, ...

  10. 【Java基础】StringTokenizer用法

    写在前面 因为最近在接触hadoop的东西,看示例WordCount的时候里面有一个StringTokenizer的东西特地看了一下 The string tokenizer class allows ...