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 ...
随机推荐
- link标签的rel属性
<link>标签定义了当前文档与 Web 集合中其他文档的关系.link 元素是一个空元素,它仅包含属性.此元素只能存在于 head 部分,不过它可出现任何次数.在 HTML 中,< ...
- [ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)
Ignatius and the Princess IV Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32767K (Ja ...
- CISCO Configuration Examples and TechNotes
from: http://www.cisco.com/c/en/us/tech/ip/ip-routing/tech-configuration-examples-list.html Border ...
- HDFS源码分析数据块复制之PendingReplicationBlocks
PendingReplicationBlocks实现了所有正在复制的数据块的记账工作.它实现以下三个主要功能: 1.记录此时正在复制的块: 2.一种对复制请求进行跟踪的粗粒度计时器: 3.一个定期识别 ...
- uva 12083 Guardian of Decency (二分图匹配)
uva 12083 Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. ...
- [python学习] 简单爬取图片站点图库中图片
近期老师让学习Python与维基百科相关的知识,无聊之中用Python简单做了个爬取"游讯网图库"中的图片,由于每次点击下一张感觉很浪费时间又繁琐.主要分享的是怎样爬取HTML的知 ...
- 【BZOJ4999】This Problem Is Too Simple! 离线+树状数组+LCA
[BZOJ4999]This Problem Is Too Simple! Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2 ...
- adjA=(detA)A-1
A–>adjA 连续性 反函数
- Grunt学习笔记【4】---- 通配符和模板
本文主要讲通配符和模板的基本使用方法. 一 通配符 通常分别指定所有源文件路径是不切实际的,因此Grunt通过内置支持node-glob 和 minimatch 库来匹配文件名(又叫作globbing ...
- 如何在MySQL中分配innodb_buffer_pool_size
如何在MySQL中分配innodb_buffer_pool_size innodb_buffer_pool_size是整个MySQL服务器最重要的变量. 1. 为什么需要innodb buffer p ...