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.

  队伍训练时做到的题目,好不容易推出了dp公式并且想到了斜率优化,结果犯了SB错误导致一直错。。。

  题目大致就是几只猫在一些地方,然后让人去收猫。。。饿还是看题吧。。。

  直接对于某个位置的猫的 t 减去到这个位置的时间就好,问题就转换成了有一些猫每个猫都有一个值,然后给P个人分别一个值,然后每个猫的找到比他大的最近的那个人的值,然后相减,累加每个猫的,让总和最小。。。饿,表达稍微比较烂。。。

  这里考虑到每个人的值一定是某只猫的值H,不然向下移动一点点可以更优。

  对每个猫的值进行排序,然后从左到右dp,

  dp[i][j]表示前i个猫,使用了j个人,的最小值。。。

  然后递推的话 dp[i][j]=min{ dp[x][j-1]+(i-x)H[i]-S[i]+S[x] };

  S表示H的前缀和。

  然后转换一下变成 min{ dp[x][j-1]+S[x]-xH[i] } + iH[i]-S[i];

  然后 Y[x]=dp[x][j-1] ,X[x]=x;

  然后就是经典斜率DP的问题了。。。

代码如下:

// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年10月09日 星期五 18时45分52秒
// File Name : B.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=; int N,M,P;
long long DP1[MaxN],DP2[MaxN];
long long *dp1,*dp2;
long long H[MaxN];
long long S[MaxN]; long long X[MaxN],Y[MaxN],cou; long long d[MaxN]; bool better(int a,int b,long long H)
{
return (Y[a]-X[a]*H)<=(Y[b]-X[b]*H);
} bool judge(long long x1,long long y1,long long x2,long long y2,long long x3,long long y3)
{
return (y1-y2)*(x2-x3)<=(y2-y3)*(x1-x2);
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); ios::sync_with_stdio(false);
cin>>N>>M>>P; d[]=;
for(int i=;i<=N;++i)
{
cin>>d[i];
d[i]+=d[i-];
}
long long a,b;
for(int i=;i<=M;++i)
{
cin>>a>>b;
H[i]=b-d[a];
} dp1=DP1;
dp2=DP2;
N=M;
sort(H+,H+N+); S[]=;
for(int i=;i<=N;++i)
{
S[i]=S[i-]+H[i];
dp1[i]=H[i]*i-S[i];
} int p;
long long TX,TY;
P=min(P,N); for(int j=;j<=P;++j)
{
cou=;
Y[]=dp1[j-]+S[j-];
X[]=j-;
p=; for(int i=j;i<=N;++i)
{
while(p<cou- && better(p+,p,H[i])) ++p;
dp2[i]=Y[p]-X[p]*H[i]-S[i]+i*H[i]; TX=i;
TY=dp1[i]+S[i]; while(cou->p && judge(TX,TY,X[cou-],Y[cou-],X[cou-],Y[cou-])) --cou;
X[cou]=TX;
Y[cou++]=TY;
}
swap(dp1,dp2);
} cout<<dp1[N]<<endl; return ;
}

(中等) CF 311B Cats Transport,斜率优化DP。的更多相关文章

  1. Codeforces 311B Cats Transport 斜率优化dp

    Cats Transport 出发时间居然能是负的,我服了... 卡了我十几次, 我一直以为斜率优化写搓了. 我们能得出dp方程式 dp[ i ][ j ] = min(dp[ k ][ j - 1 ...

  2. CodeForces 311 B Cats Transport 斜率优化DP

    题目传送门 题意:现在有n座山峰,现在 i-1 与 i 座山峰有 di长的路,现在有m个宠物, 分别在hi座山峰,第ti秒之后可以被带走,现在有p个人,每个人会从1号山峰走到n号山峰,速度1m/s.现 ...

  3. CF311B Cats Transport 斜率优化DP

    题面:CF311B Cats Transport 题解: 首先我们观察到山与距离其实是没有什么用的,因为对于任意一只猫,我们都可以直接算出如果有一个人要恰好接走它,需要在哪一时刻出发,我们设第i只猫对 ...

  4. CF331B Cats Transport[斜率优化dp+贪心]

    luogu翻译 一些山距离起点有距离且不同,m只猫要到不同的山上去玩ti时间,有p个铲屎官人要去把所有猫接走,步行速度为1单位每秒,从1走到N座山不停下,必须在猫玩完后才可以把他带走.可以提前出发.问 ...

  5. 【题解】Cats Transport (斜率优化+单调队列)

    [题解]Cats Transport (斜率优化+单调队列) # When Who Problem Lang Verdict Time Memory 55331572 Jun/09/2019 19:1 ...

  6. $CF311B\ Cats\ Transport$ 斜率优化

    AcWing Description Sol 设f[i][j]表示前i个饲养员接走前j只猫咪的最小等待时间. 要接到j猫咪,饲养员的最早出发时间是可求的,设为d: $ d[j]=Tj-\sum_{k= ...

  7. 2018.09.07 codeforces311B. Cats Transport(斜率优化dp)

    传送门 斜率优化dp好题. 对于第i只猫,显然如果管理员想从出发开始刚好接到它,需要在t[i]=h[i]−dist(1,i)" role="presentation" s ...

  8. CF-311B Cats Transport(斜率优化DP)

    题目链接 题目描述 小S是农场主,他养了 \(M\)只猫,雇了 \(P\) 位饲养员. 农场中有一条笔直的路,路边有 \(N\) 座山,从 \(1\) 到 \(N\)编号. 第 \(i\) 座山与第 ...

  9. 斜率优化DP学习笔记

    先摆上学习的文章: orzzz:斜率优化dp学习 Accept:斜率优化DP 感谢dalao们的讲解,还是十分清晰的 斜率优化$DP$的本质是,通过转移的一些性质,避免枚举地得到最优转移 经典题:HD ...

随机推荐

  1. PHP数学函数试题

    1.求绝对值的函数是什么? 2.在任意进制之间转换数字的函数是什么? 3.二进制转换为十进制,十进制转换为二进制,十六进制转换为十进制,十进制转换为十六进制,八进制转换为十进制,十进制转换为八进制的函 ...

  2. SQL 优化案例 1

    create or replace procedure SP_GET_NEWEST_CAPTCHA( v_ACCOUNT_ID in VARCHAR2, --接收短信的手机号 v_Tail_num i ...

  3. HDU 2802 F(N) 数论+打表

    题目大意:f[n]-n^3=f[n-2]-(n-1)^3 (n >=3),f[1]=1,f[2]=7,求f[n]. 题目思路:将n^3移到到等式右边化简的到:f[n]=f[n-2]+3n*(n- ...

  4. CentOS下载及版本选择-CentOS LiveCD、LiveDVD和BinDVD区别

    1.CentOS系统镜像有两个,安装系统只用到第一个镜像即CentOS-6.x-i386-bin-DVD1.iso(32位)或者CentOS-6.x-x86_64-bin-DVD1.iso(64位), ...

  5. 创建zend framework 项目要注意的

    1.必须要设置变量环境 我的电脑右击-属性-高级-环境变量 则在环境变量中添加 变量名:PATH 环境值:D:\phpserver\php5.4;D:\ZendFramework\bin 把php.e ...

  6. boost库之geometry

    环境:win732位旗舰版.VS2010旗舰版.boost 1.55.0版本.坐标系为MM_TEXT Geometry是一个开源的几何计算库,包含了几何图形最基本的操作(也支持复杂的操作),下面我们看 ...

  7. Dom++完美版得到元素到html的距离6/4/21

    function getTop(obj) { var pos={left:0,top:0}; while(obj) { pos.left+=obj.offsetLeft; pos.top+=obj.o ...

  8. 报错:error C2229: class ' ' has an illegal zero-sized array 引发的思考

    问题:

  9. 动态规划1-----------poj1080

    #include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm> us ...

  10. Safari WebApp 模拟 原声APP禁止打开新窗口JS代码

    if(("standalone" in window.navigator) && window.navigator.standalone) { var noddy, ...