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 ...
随机推荐
- 用递归法将一个整数n转换成字符串。
用递归法将一个整数n转换成字符串. 比如,输入483,应输出字符串"483".n的位数不确定,能够是随意位数的整数. #include "stdafx.h" # ...
- tf树
tf变换(1) TF库的目的是实现系统中任一个点在所有坐标系之间的坐标变换,也就是说,只要给定一个坐标系下的一个点的坐标,就能获得这个点在其他坐标系的坐标. 使用tf功能包,a. 监听tf变换: ...
- arcgis水文分析
前言 1.在开始之前首先需要注意几点: 1.arcgis 需要 python2.7 的支持,并有必要的模块库,请一定注意避免与其他软件冲突,例如tecplot 2009 需要python2.5的支持, ...
- 织梦在广告(myad)中使用css样式
使用单引号,以及只有style这一个属性
- SpringBoot启动流程分析(一):SpringApplication类初始化过程
SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...
- 设置mysql隔离级别
1.查看当前会话隔离级别 select @@tx_isolation; 2.查看系统当前隔离级别 select @@global.tx_isolation; 3.设置当前会话隔离级别 set sess ...
- 深入浅出Attribute (一)
正文: 什么是Attribute?Attribute是干什么使的?Attribute与Property到底有什么区别?…… 长久以来,这些问题一直困扰着并不怎么广大的C#初学者.原因大概有两个,一是A ...
- 【Selenium + Python】路径报错之OSError: [Errno 22] Invalid argument: './t/report/2018-03-23_11:03:12_report.html'
现象: 此问题真的是太痛苦了,查了好多资料是说路径的问题,结果还是报错,后来一点点的排查才发现原来是!!!!!! 废话不多说上原来代码: if __name__ == '__main__': star ...
- JavaScript -- JavaScript DOM 编程艺术(第2版)
/* 渐进增强 平稳退化 网页 结构层(structural layer): HTML 表示层(presentation layer): CSS <link rel="styleshe ...
- PHP与js之间的交互
<?php//在php中药想使用jquery,首先需要导入jquery类库 echo "<script src='".base_url('static')." ...