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 ...
随机推荐
- Deploying Docker images via SSH
Original URL:https://advancedweb.hu/2015/04/14/deploying-docker-images-via-ssh/ Background When we b ...
- Robot Framework使用Phantomjs进行无界面UI自动化测试
Robot Framework 是一款关键字驱动的验收自动化测试框架,现在在国内使用的越来越广泛了.一种通用的Web UI自动化测试解决方案是Robot Framework+Selenium2Libr ...
- git stash 保存当前工作状态
1. git stash 暂存当前工作状态 2. git stash list 查看暂存列表 3. git stash save 'title' 暂存工作状态并添加说明 4. git stash ...
- 卸载gnu gcj
麻辣个鸡的,我在Linux上安装的jkd版本是1.8,然后可能是之后安装了GCC吧,他大爷的,java版本变成了1.5.这个残酷的事实是在我写练习Package的测试文件的时候搞得. 机智的看了一下j ...
- 有两个好友A和B,住在一片长有蘑菇的由n*m个方格组成的草地,A在(1,1),B在(n,m)。现在A想要拜访B,由于她只想去B的家,所以每次她只会走(i,j+1)或(i+1,j)这样的路线,在草地上有k个蘑菇种在格子里(多个蘑菇可能在同一方格),问:A如果每一步随机选择的话(若她在边界上,则只有一种选择),那么她不碰到蘑菇走到B的家的概率是多少?
第二种方法:首先分析题意,可用概率的方法来计算,做了好几道百度的题目,觉得大多数是再考概率论,所以首先要弄懂题意,最后做题前把公式写出来,这样编码时才能游刃有余. 本题中下面的第一种用迭代枚举的方法来 ...
- ubuntu apt 主要命令及参数
1. apt-cache search package 搜索安装包 2. apt-cache search all 搜索所有安装包 3. apt-cache show package 显示安装包信息 ...
- react build和server start
先到项目目录build项目 npm run build 项目会打包到dist文件夹下 index.html和index.js等 react的项目build后不能直接访问的问题 先执行 npm inst ...
- python 基础 2.1 if 流程控制(一)
一.if else 1.if 语句 if expression: //注意if后有冒号,必须有 statement(s) //相对于if缩进4个空格 注:pytho ...
- 【BZOJ1097】[POI2007]旅游景点atr 最短路+状压DP
[BZOJ1097][POI2007]旅游景点atr Description FGD想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺 ...
- 【BZOJ4519】[Cqoi2016]不同的最小割 最小割树
[BZOJ4519][Cqoi2016]不同的最小割 Description 学过图论的同学都知道最小割的概念:对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分 ...