CodeForces - 311B:Cats Transport (DP+斜率优化)
Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The feeders live in hill 1.
One day, the cats went out to play. Cat i went on a trip to hill hi, finished its trip at time ti, and then waited at hill hi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 to n without waiting at a hill and takes all the waiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want.
For example, suppose we have two hills (d2 = 1) and one cat that finished its trip at time 3 at hill 2 (h1 = 2). Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can't take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units.
Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized.
Input
The first line of the input contains three integers n, m, p (2 ≤ n ≤ 105, 1 ≤ m ≤ 105, 1 ≤ p ≤ 100).
The second line contains n - 1 positive integers d2, d3, ..., dn (1 ≤ di < 104).
Each of the next m lines contains two integers hi and ti (1 ≤ hi ≤ n, 0 ≤ ti ≤ 109).
Output
Output an integer, the minimum sum of waiting time of all cats.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
4 6 2
1 3 5
1 0
2 1
4 9
1 10
2 10
3 12
3
题意:有一些猫,放在一些位置,人走到每个猫的时间已知,给个猫出现的时间已知,假设派出一个人,可以自由安排其出发时间,沿途已经出现的猫pick掉,猫等待的时间是被pick的时间减去出现的时间t,t>=0。现在有P个人,问总时间T最小是多少。
思路:对猫: 人time+猫dis-猫time。把c[i]-t[i]排序,那么就成为了把M个数划分位P个区间,每个区间的值=所有数与最大数的差值。
DP[i][j]=min DP[k][j-1]+c[i]*(i-k)-(sum[i]-sum[k]);
转化:B=-c[i]*k+(dp[k][j-1]+sum[k])+c[i]*i-sum[i];
方程的斜率为k=c[i];y= (dp[k][j-1]+sum[k]) ;截距B=DP[i][j];常数C=c[i]*i-sum[i];
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
ll d[maxn],c[maxn],sum[maxn],dp[maxn][],t;
int q[maxn],head,tail;
ll getans(int i,int j,int k){ return dp[k][j-]+c[i]*(i-k)-(sum[i]-sum[k]); }
ll Y(int k,int j){ return dp[k][j-]+sum[k]; }
int main()
{
int N,M,P,i,j,h;
scanf("%d%d%d",&N,&M,&P);
for(i=;i<=N;i++) scanf("%I64d",&d[i]),d[i]+=d[i-];
for(i=;i<=M;i++){
scanf("%d%I64d",&h,&t);
c[i]=t-d[h];
}
sort(c+,c+M+);
for(i=;i<=M;i++) sum[i]=sum[i-]+c[i];
for(i=;i<=M;i++) dp[i][]=c[i]*(i-)-sum[i-];
for(j=;j<=P;j++){
head=tail=;
for(i=;i<=M;i++){
while(tail>head&&Y(q[head+],j)-Y(q[head],j)<c[i]*(q[head+]-q[head])) head++;
dp[i][j]=getans(i,j,q[head]);
while(tail>head&&(Y(i,j)-Y(q[tail],j))*(q[tail]-q[tail-])<(Y(q[tail],j)-Y(q[tail-],j))*(i-q[tail])) tail--;
q[++tail]=i;
}
}
printf("%I64d\n",dp[M][P]);
return ;
}
经验:弹出队首时,可以直接通过比较结果获得。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
ll d[maxn],c[maxn],sum[maxn],dp[maxn][],t;
int q[maxn],head,tail;
ll getans(int i,int j,int k){ return dp[k][j-]+c[i]*(i-k)-(sum[i]-sum[k]); }
ll Y(int k,int j){ return dp[k][j-]+sum[k]; }
int main()
{
int N,M,P,i,j,h;
scanf("%d%d%d",&N,&M,&P);
for(i=;i<=N;i++) scanf("%I64d",&d[i]),d[i]+=d[i-];
for(i=;i<=M;i++){
scanf("%d%I64d",&h,&t);
c[i]=t-d[h];
}
sort(c+,c+M+);
for(i=;i<=M;i++) sum[i]=sum[i-]+c[i];
for(i=;i<=M;i++) dp[i][]=c[i]*(i-)-sum[i-];
for(j=;j<=P;j++){
head=tail=;
for(i=;i<=M;i++){
while(tail>head&&getans(i,j,q[head])>getans(i,j,q[head+])) head++;
dp[i][j]=getans(i,j,q[head]);
while(tail>head&&(Y(i,j)-Y(q[tail],j))*(q[tail]-q[tail-])<(Y(q[tail],j)-Y(q[tail-],j))*(i-q[tail])) tail--;
q[++tail]=i; //队首可以getans维护,队尾不行,必须维护斜率!
}
}
printf("%I64d\n",dp[M][P]);
return ;
}
CodeForces - 311B:Cats Transport (DP+斜率优化)的更多相关文章
- Codeforces 311B Cats Transport【斜率优化DP】
LINK 题目大意 有一些猫,放在一些位置,人一步移动一个位置 给出每个猫出现的时间,每个人可以自由安排其出发时间,沿途已经出现的猫捡起,猫等待的时间是被减去的时间减去出现的时间 猫可以等人,人不能等 ...
- (中等) CF 311B Cats Transport,斜率优化DP。
Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight r ...
- Codeforces 311B Cats Transport 斜率优化dp
Cats Transport 出发时间居然能是负的,我服了... 卡了我十几次, 我一直以为斜率优化写搓了. 我们能得出dp方程式 dp[ i ][ j ] = min(dp[ k ][ j - 1 ...
- 2018.09.07 codeforces311B. Cats Transport(斜率优化dp)
传送门 斜率优化dp好题. 对于第i只猫,显然如果管理员想从出发开始刚好接到它,需要在t[i]=h[i]−dist(1,i)" role="presentation" s ...
- CF-311B Cats Transport(斜率优化DP)
题目链接 题目描述 小S是农场主,他养了 \(M\)只猫,雇了 \(P\) 位饲养员. 农场中有一条笔直的路,路边有 \(N\) 座山,从 \(1\) 到 \(N\)编号. 第 \(i\) 座山与第 ...
- Cats transport(codeforces311B)(斜率优化)
\(Cats Transport\) 感觉这道题题面不好讲,就自翻了一个新的,希望有助于大家理解其思路: 大致题意: \(wch\) 的家里有 \(N\) 座山(山呈直线分布,第 \(i-1\) 座山 ...
- CF311B Cats Transport(斜率优化)
题目描述 Zxr960115 是一个大农场主.他养了m只可爱的猫子,雇佣了p个铲屎官.这里有一条又直又长的道路穿过了农场,有n个山丘坐落在道路周围,编号自左往右从1到n.山丘i与山丘i-1的距离是Di ...
- 【BZOJ-4518】征途 DP + 斜率优化
4518: [Sdoi2016]征途 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 230 Solved: 156[Submit][Status][ ...
- 【BZOJ-3437】小P的牧场 DP + 斜率优化
3437: 小P的牧场 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 705 Solved: 404[Submit][Status][Discuss ...
随机推荐
- 浅谈js中的MVC
MVC是什么? MVC是一种架构模式,它将应用抽象为3个部分:模型(数据).视图.控制器(分发器) 本文将用一个经典的例子todoList来展开 一个事件发生的过程(通信单向流动): 1.用户在视图V ...
- Nginx https免费SSL证书配置指南
生成证书 $ cd /usr/local/nginx/conf $ openssl genrsa -des3 -out server.key 1024 $ openssl req -new -key ...
- 数据挖掘之pandas
sdata={'语文':89,'数学':96,'音乐':39,'英语':78,'化学':88} #字典向Series转化 @@ >>> studata=Series(sdata) & ...
- 用汇编的角度剖析c++的virtual
多态是c++的关键技术,背后的机制就是有一个虚函数表,那么这个虚函数表是如何存在的,又是如何工作的呢? 当然不用的编译器会有不同的实现机制,本文只剖析vs2015的实现. 单串继承 首先看一段简单的代 ...
- 10个经典Java面试题
1.Java的HashMap是怎样工作的? HashMap是一个针对数据结构的键值.每一个键都会有对应的值.关键是识别这种值. HashMap 基于 hashing 原理,我们通过 put ()和 g ...
- erlang程序优化点的总结
注意,这里只是给出一个总结,具体性能需要根据实际环境和需要来确定 霸爷指出,新的erlang虚拟机有很多调优启动参数,今后现在这个方面深挖一下. 1. 进程标志设置: 消息和binary内存:erla ...
- Docker入门系列6 如何打开多个终端进入Docker容器
Docker容器运行后,如何进入容器进行操作呢?起初我是用SSH.如果只启动一个容器,用SSH还能应付,只需要将容器的22端口映射到本机的一个端口即可.当我启动了五个容器后,每个容器默认是没有配置SS ...
- Frege-基于JVM的类Haskell纯函数式编程语言
Frege是一门受Haskell语言启示而设计的纯函数式编程语言.Frege程序会被编译为Java,并执行于JVM上.它与Haskell是如此的类似.以至于有人称它为JVM上的Haskell.取Fre ...
- 转载 ----Android学习笔记 - 蓝牙篇 (Bluetooth)
1.什么是蓝牙 Bluetooth是目前使用的最广泛的无线通讯协议之一 主要针对短距离设备通讯(10米) 常用于连接耳机.鼠标和移动通讯设备等 2.发现周围蓝牙设备 BluetoothAd ...
- android Bluetooth-蓝牙
bluetooth 一.开启蓝牙 1.获取BluetoothAdapter BluetoothAdapter.getDefaultAdapter() 2.判断手机设备是否 有蓝牙模块 3.开启蓝牙设备 ...