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. C#当中的多线程_任务并行库(中)

    发现自己有点懒了!也可能是越往后越难了,看书理解起来有点费劲,所以这两天就每天更新一点学习笔记吧. 4.5 将APM模式转化为任务 书上提供的三种方式 方式一: class Program       ...

  2. android - android Couldn't load runtimecore_java from loader

    在写Arcgis Android 或百度Android的时候,有时会报诸如,java.lang.UnsatisfiedLinkError:android Couldn't load runtimeco ...

  3. 使用本地光盘安装Microsoft .NET Framework 3.5 for Win8/WinServer2012

    如何使用本地源安装Microsoft .NET Framework 3.5 作为SQL Server 2012的 必要组件,校验组件过程有个小BUG,即使没有安装也通过,但会卡在安装环节(enabli ...

  4. 研究WCF并发及处理能力的控制

    WCF 跟并发 性能相关的几个配置:1.系统控制的客户端网络连接并发(如果服务端也需要并发请求的话这个参数也是需要的):          <configuration>          ...

  5. ES 中文分词

    一.大名鼎鼎的中文插件IK的安装配置 1. 在插件目录中建立IK的目录 mkdir $ES_HOME/plugins/analysis-ik 2. 下载IK 的类库jar 文件到IK目录 cd $ES ...

  6. kaptcha小案例(转)

    使用kaptcha生成验证码 kaptcha是一个简单好用的验证码生成工具,通过配置,可以自己定义验证码大小.颜色.显示的字符等等.下面就来讲一下如何使用kaptcha生成验证码以及在服务器端取出验证 ...

  7. APACHE 与IIS同时存在的情况下,给APACHE添加反向代理 共用80端口

    一.首先打开IIS,将IIS的端口改成81,不要让IIS占用了80端口 二.打开APACHE的httpd.conf配置文件,将里面的端口配置成80 三.打开APACHE的虚拟目录配置文件,如:http ...

  8. 使用APPLICATION制作缓存,转存一下,有一段写的还可以。

    public sealed class CacheManager   {   private HttpApplicationState appPool = null;   /// <summar ...

  9. C++静态成员函数访问非静态成员的几种方法

    大家都知道C++中类的成员函数默认都提供了this指针,在非静态成员函数中当你调用函数的时候,编译器都会“自动”帮你把这个this指针加到函数形参里去.当然在C++灵活性下面,类还具备了静态成员和静态 ...

  10. 让QMainWindow也表现出QDialog的exec函数的特征

    前几天在做毕业设计项目的时候,使用的PyQt4,想实现这么样一个功能: 场景描述:主窗口a(QMainWindow类型)和主窗口b(QMainWindow),b是通过a窗口中某一个按钮弹出来的. 功能 ...