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. sql求日期

    2.求以下日期SQL: 昨天 select convert(varchar(10),getdate() - 1,120) 明天 select convert(varchar(10),getdate() ...

  2. 1.2 eclipse使用 :working set

    working set可以是相当于文件夹~~有多个project时, 分别存放在不同的 workingset下,可以方便管理 新建或编辑 working set时,需要记住选择project *可参照 ...

  3. LightOJ 1259 Goldbach`s Conjecture 素数打表

    题目大意:求讲一个整数n分解为两个素数的方案数. 题目思路:素数打表,后遍历 1-n/2,寻找方案数,需要注意的是:C/C++中 bool类型占用一个字节,int类型占用4个字节,在素数打表中采用bo ...

  4. IO流的操作规律

    输入流和输出流相对于内存设备而言. 将外设中的数据读取到内存中:输入将内存的数写入到外设中:输出. 字符流的由来:其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表.获取对应的文字.在 ...

  5. Power oj2470/DFS

    题目链接 2469: C 小Y的难题(1) Time Limit: 1000 MS Memory Limit: 65536 KB Total Submit: 9 Accepted: 7 Page Vi ...

  6. property函数

    __metaclass__=type class Rectangle:     def __init__(self):             self.width=0             sel ...

  7. java 设计模式之工厂模式与反射的结合

    工厂模式: /**  * @author Rollen-Holt 设计模式之 工厂模式  */   interface fruit{     public abstract void eat(); } ...

  8. panel 绑定鼠标滚轮事件

    void formsample_mousewheel(object sender, MouseEventArgs e) { //获取光标位置 Point mousepoint = new Point( ...

  9. Python 安装matplotlib,six,dateutil,pyparsing 完整过程

    [摘要:正在做词频剖析的时间,须要用matlotlib 做图表,柱状图啥的,因而便最先了一个又一个的装置库的进程 由于matplotlib 须要依附很多其他科教盘算的第三圆库,须要一个一个的装置了.. ...

  10. ZOJ 2872 Binary Partitions

    先写一个完全背包,然后找规律,然后打表. #include<cstdio> #include<cstring> #include<cmath> #include&l ...