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实现队列的原理

    关于的队列的介绍,我这里就不多讲了,随便百度一下都很多 用过laravel框架的童鞋都知道其自带队列功能,之前我很费解,PHP只是一个脚本,有超时机制 为什么能不停的去执行队列呢? 带着这个问题,在网 ...

  2. Mac上安装Tomcat

    参考文档:http://blog.csdn.net/j2ee_me/article/details/7928493 0.如果java没有被初始安装,所以你要自己去找Apple的安装程序这里来安装,或者 ...

  3. 为什么要在onNewIntent的时候要显示的去调用setIntent

    一.原因: 当调用到onNewIntent(intent)的时候,需要在onNewIntent() 中使用setIntent(intent)赋值给Activity的Intent.否则,后续的getIn ...

  4. 笨方法学python--字符串和文本

    1print 变量 abc = False joke_string = "Isn't that joke so funny?! %r" print joke_string %abc ...

  5. 关于C++ const

    1.Const用途 No. 用途 使用范围 参考代码 1 类型检查 参数传递 void func(const int i){ ... } 2 节省空间,避免不必要的内存分配 代替#define #de ...

  6. android 去掉listview之间的黑线

    方法1:listView.setDividerHeight(0);方法2:this.getListView().setDivider(null);方法3:android:divider="@ ...

  7. nginx slab内存管理

    本来这一篇作为nginx系列的开头是不合适的,不过由于nginx进程框架自己的梳理还没完成,这部分又刚好整理完了,就从这开始吧.这儿谈的是nginx的slab的内存管理方式,这种方式的内存管理在ngi ...

  8. js zhi网马

      大家对木马都不陌生了,它可能要算是计算机病毒史上最厉害的了,相信会使木马的人千千万万,但是 有很多人苦于怎么把木马发给对方,现在随着计算机的普及,在网络上我相信很少有人会再轻易的接收 对方的文件了 ...

  9. 开源企业管理软件 ONES

    ONES 不是 ONS,基于AngularJS + ThnkPHP开发的企业管理系统平台,名字可以理解为ONES is a Niubility ERP System 或者 ONES Notonly a ...

  10. HDU 5903 (DP)

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...