HDU 3045 Picnic Cows(斜率优化DP)
Picnic Cows
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2192 Accepted Submission(s): 675
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.
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.
line for each test case, containing one integer means the minimum of
the TOTAL reduced Moo~s to group N cows to several teams.
8 5 6 2 1 7 6
【思路】
斜率优化+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)的更多相关文章
- hdu 3045 Picnic Cows(斜率优化DP)
题目链接:hdu 3045 Picnic Cows 题意: 有n个奶牛分别有对应的兴趣值,现在对奶牛分组,每组成员不少于t, 在每组中所有的成员兴趣值要减少到一致,问总共最少需要减少的兴趣值是多少. ...
- HDU 3045 - Picnic Cows - [斜率DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3045 It’s summer vocation now. After tedious milking, ...
- HDU3045 Picnic Cows —— 斜率优化DP
题目链接:https://vjudge.net/problem/HDU-3045 Picnic Cows Time Limit: 8000/4000 MS (Java/Others) Memor ...
- hdu 3507 Print Article(斜率优化DP)
题目链接:hdu 3507 Print Article 题意: 每个字有一个值,现在让你分成k段打印,每段打印需要消耗的值用那个公式计算,现在让你求最小值 题解: 设dp[i]表示前i个字符需要消耗的 ...
- HDU 2829 Lawrence(斜率优化DP O(n^2))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...
- 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]$为答案. 然后斜率优化就可以 ...
- HDU 3045 picnic cows(斜率DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3045 题目大意:有n个数,可以把n个数分成若干组,每组不得小于m个数,每组的价值=除了该组最小值以外每 ...
- HDU 3401 Trade(斜率优化dp)
http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:有一个股市,现在有T天让你炒股,在第i天,买进股票的价格为APi,卖出股票的价格为BPi,同时最多买 ...
- HDU 4258 Covered Walkway 斜率优化DP
Covered Walkway Problem Description Your university wants to build a new walkway, and they want at ...
随机推荐
- SQL Server备份还原数据库中的小把戏
备份数据库时出现一个不太了解的错误 ,错误信息“is formatted to support 1 media families, but 2 media families are expected ...
- Object-C — KVO & oc通知
键值观察(KVO)是基于键值编码的一种技术. 利用键值观察可以注册成为一个对象的观察者,在该对象的某个属性变化时收到通知. 被观察对象需要编写符合KVC标准的存取方法,编写键值观察分为以下三步: (1 ...
- C字符串总结+字符串库实现(增,改,删,查):
<一>,字符指针&字符数组 两者形式: 字符指针:char *p; 字符数组:char str[100]; 两者区别: 字符指针p是变量: 字符数组str是常量: 访问元素方式: ...
- jQuery的延迟对象
之前看别人的demo,发现在延迟对象被resolve时要执行的代码,有时会写在deferred.then方法里执行,有时会写在deferred.done方法里执行. 这让对延迟对象一知半解的我非常困惑 ...
- ListView复用和优化详解
我们每一个Android开发人员对ListView的使用肯定是很熟悉的,然而多少人能真正的懂ListView的缓存机制呢,说白了就是ListView为了提高效率,而内部实现的一种优化,牺牲一点内存.而 ...
- java执行机制
java代码编译是由Java源码编译器来完成,流程图如下所示: Java字节码的执行是由JVM执行引擎来完成,流程图如下所示: Java代码编译和执行的整个过程包含了以下三个重要的机制: Java源码 ...
- Day21 Django之Form文件上传、原生Ajax和实现抽屉实例
一.Form文件上传 """ Django settings for prev_chouti project. Generated by 'django-admin st ...
- WPF后台访问XAML元素
当我们需要从后台访问xaml文件时,我们可以通过这样的方式来操作: private void button1_Click(object sender, RoutedEventArgs e) { Sys ...
- C# mvc 验证码2
public class ValidateCode { /// <summary> /// 產生圖形驗證碼. /// </su ...
- 趣味C程序100.1 .1 绘制余弦曲线
说明:1.本问题来源于<C语言经典.趣味.实用程序设计编程百例精解>,所有程序为本人自己编写.与原程序不同之处作有标记. 2.本系列所有程序均使用codeblocks编译,操作系统为Win ...