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 ...
随机推荐
- jsp中<c:if>与<s:if>的区别
<c:if>时jstl标签,一般与el表达式一起使用,参考http://www.360doc.com/content/11/1121/16/7874148_166229306.shtml ...
- Codevs 1010 过河卒 2002年NOIP全国联赛普及组
1010 过河卒 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 如图,A 点有一个过河卒 ...
- HDU 题目分类
转载自新浪博客,, http://blog.sina.com.cn/s/blog_71ded6bf0100tuya.html 基础题: 1000.1001.1004.1005.1008.1012.10 ...
- sql 建立数据库,表格,索引,主键
---- 数据库: `message_db`-- -- --------------------------------------------------------create database ...
- 微信菜单开发:使用PHP数组来定义微信菜单
目前使用数组的方式来定义,然后在通过json_encode函数来转JSON $menuJson=array(); $menuJson['button'][]=array('name'=>urle ...
- [WinJS] Promise 用法
初学 WinJS, 可能对 Promise 的用法不大清楚,本文简要说明一下 WinJS中 promise 的基本用法. 主要参考自:http://try.buildwinjs.com/#promis ...
- svn修改密码跟账户
在co的时候直接输入账户跟密码 svn co ${SVNURL} ./ --username **--password *** 或者删除缓存文件,在Linux上面 删除~/.subversion/a ...
- 2016030204 - git和github结合
1.下载和安装git客户端 参考:http://www.cnblogs.com/zhtzyh2012/p/5232291.html 2.github上创建项目 参考:http://www.cnblog ...
- 并发视频,怎么hold住高并发
http://v.qq.com/boke/page/z/w/s/z0110e15rws.html http://weibo.com/codebox
- bzoj 3632: 外太空旅行 最大团
3632: 外太空旅行 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 389 Solved: 129[Submit][Status] Descript ...