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 ...
随机推荐
- 【转载】ASP.Net请求处理机制初步探索之旅 - Part 3 管道
开篇:上一篇我们了解了一个ASP.Net页面请求的核心处理入口,它经历了三个重要的入口,分别是:ISAPIRuntime.ProcessRequest().HttpRuntime.ProcessReq ...
- 在EA中用ER图生成数据库
ER图 E-R图也称实体-联系图(Entity Relationship Diagram).提供了表示实体类型.属性和联系的方法.用来描写叙述现实世界的概念模型. 实体就是看的见摸得着或者能被人感知接 ...
- Html5 meta 笔记
摘抄:原文地址:http://www.kmapk.com/html/help/02/127.html 一.天猫 <title>天猫触屏版</title> <meta co ...
- 非spring托管的类使用spring脱管的类。
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationConte ...
- partition by和group by对比
今天大概弄懂了partition by和group by的区别联系. 1. group by是分组函数,partition by是分析函数(然后像sum()等是聚合函数): 2. 在执行顺序上, 以下 ...
- python学习(十四)面向对象
Python中的面向对象,先写类,会生成类对象,类对象然后创建对象,对象就可以拿来用了. Python支持多重继承. class语句创建类对象,并将其赋值给变量名. class语句内的赋值语句会创建类 ...
- 世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么?
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- FPGA学习记录 - Quartus II 未使用管脚设置为三态输入
未使用管脚设置为三态输入 Assignments -> Device 或双击器件
- 计算机网络--DNS
1.域名系统DNS(domain name system)是因特网使用的命名系统,用来把便于人们时用的机器名字转换为IP地址.因特网的域名系统DNS被设计成一个联机分布式数据库系统,并采用客户服务器方 ...
- 安装配置Django
安装配置Django 以下是基于python3.5 pip install Django 把python环境目录Scripts配置到环境变量,主要在命令行中随时可以使用django-admin 验证 ...