Picnic Cows

                    Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                          Total Submission(s): 2192    Accepted Submission(s): 675

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
 

【思路】

斜率优化+DP

首先问一句Carolina和John什么关系 ヘ(;´Д`ヘ)

不难设计出转移方程为:

f[i]=min{ f[j]+C[i]-C[j]+(i-j)*X[j+1] } T<=j<=i-T

其中C表示X的前缀和。

如果j>k且决策j优于决策k则有

f[j]-f[k]+C[k]-C[j]-k*X[k+1]+j*X[j+1]<i*(X[j+1]-X[k+1])

维护指定区间内的下凸包即可。

需要注意的是输入输出用int64,而且斜率部分不能用之前直接除的写法了,因为X有long long,可能误差会比较大,改用化除为乘的方法。

   坑了我好长时间 T_T

【代码】

 #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long LL;
typedef long double LD;
const int N = +; int L,R,n,T,q[N]; LL A[N],C[N],f[N];
LL UP(int j,int k) {
return f[j]-C[j]+j*A[j+]-(f[k]-C[k]+k*A[k+]);
}
LL DN(int j,int k) {
return A[j+]-A[k+];
}
int main() {
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
while(scanf("%d%d",&n,&T)==) {
for(int i=;i<=n;i++) scanf("%I64d",&A[i]);
sort(A+,A+n+);
for(int i=;i<=n;i++) C[i]=C[i-]+A[i];
L=R=;
for(int i=;i<=n;i++) {
while(L<R && UP(q[L+],q[L])<=i*DN(q[L+],q[L])) L++;
int t=q[L],j;
f[i]=f[t]-C[t]+t*A[t+]-i*A[t+]+C[i];
if((j=i-T+)>=T) {
while(L<R && UP(j,q[R])*DN(q[R],q[R-])<=UP(q[R],q[R-])*DN(j,q[R])) R--;
q[++R]=j;
}
}
printf("%I64d\n",f[n]);
}
return ;
}

HDU 3045 Picnic Cows(斜率优化DP)的更多相关文章

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

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

  2. HDU 3045 - Picnic Cows - [斜率DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3045 It’s summer vocation now. After tedious milking, ...

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

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

  4. hdu 3507 Print Article(斜率优化DP)

    题目链接:hdu 3507 Print Article 题意: 每个字有一个值,现在让你分成k段打印,每段打印需要消耗的值用那个公式计算,现在让你求最小值 题解: 设dp[i]表示前i个字符需要消耗的 ...

  5. HDU 2829 Lawrence(斜率优化DP O(n^2))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...

  6. 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]$为答案. 然后斜率优化就可以 ...

  7. HDU 3045 picnic cows(斜率DP)

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

  8. HDU 3401 Trade(斜率优化dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:有一个股市,现在有T天让你炒股,在第i天,买进股票的价格为APi,卖出股票的价格为BPi,同时最多买 ...

  9. HDU 4258 Covered Walkway 斜率优化DP

    Covered Walkway Problem Description   Your university wants to build a new walkway, and they want at ...

随机推荐

  1. fiddler接口测试

    浏览器中,可直接进行get接口测试:调用post方法的接口测试可用fiddler测试(当然,fiddler也支持get),如下图 [Execute]后双击左侧请求记录记录即可查看响应结果

  2. JavaEE web.xml 中ContextLoaderListener的解析

    ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在 ...

  3. iOS textfield实现一行的数字限制,超出进行弹框

    步骤一:添加textfield协议‘ @interface LsGeXingQianMingVC ()<UITextFieldDelegate> 步骤2:设置代理 _GeXingQianM ...

  4. CoreAnimation4-隐式动画和显式动画

    事务 Core Animation基于一个假设,说屏幕上的任何东西都可以(或者可能)做动画.动画并不需要你在Core Animation中手动打开,相反需要明确地关闭,否则他会一直存在. 当你改变CA ...

  5. C#输出40以内的所有奇数程序代码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  6. 常见 PL.SQL 数据库操作

    Oracle PL/SQL 1, Alt +E 2, 默认大写功能, 解析SQL原则,Comment,UnComment. 3, 触发Trig,使用Test Window. 4, Compile In ...

  7. jQuery 遍历祖先

    祖先是父.祖父或曾祖父等等. 通过 jQuery,您能够向上遍历 DOM 树,以查找元素的祖先. 向上遍历 DOM 树 这些 jQuery 方法很有用,它们用于向上遍历 DOM 树: parent() ...

  8. C语言中short的意思

    short和int等一样,是C或C++的一种内部数据类型.用于表示有符号整数.不同的是,他们在内存中所占的空间大小不同,short通常为int所占一半,也有一些实现为和int一样,但不会比int大.所 ...

  9. [学习笔记]设计模式之Composite

    为方便读者,本文已添加至索引: 设计模式 学习笔记索引 写在前面 在Composite(组合)模式中,用户可以使用多个简单的组件以形成较大的组件,而这些组件还可能进一步组合成更大的.它重要的特性是能够 ...

  10. 插入标记 方法 insertAdjacentHTML

    html5新增的插入标记方法,insertAdjacentHTML() 可以接受2个参数 插入位置和要插入的 HTML 文本.第一个参数必须是下列值之一: "beforebegin" ...