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

Input

4 6 21 3 51 02 14 91 102 103 12

Output

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=100010;
ll d[maxn],c[maxn],sum[maxn],dp[maxn][101],t;
int q[maxn],head,tail;
ll getans(int i,int j,int k){ return dp[k][j-1]+c[i]*(i-k)-(sum[i]-sum[k]); }
ll Y(int k,int j){ return dp[k][j-1]+sum[k]; }
int main()
{
    int N,M,P,i,j,h;
    scanf("%d%d%d",&N,&M,&P);
    for(i=2;i<=N;i++) scanf("%I64d",&d[i]),d[i]+=d[i-1];
    for(i=1;i<=M;i++){
        scanf("%d%I64d",&h,&t);
        c[i]=t-d[h];
    }
    sort(c+1,c+M+1);
    for(i=1;i<=M;i++) sum[i]=sum[i-1]+c[i];
    for(i=1;i<=M;i++) dp[i][1]=c[i]*(i-1)-sum[i-1];
    for(j=2;j<=P;j++){
        head=tail=0;
        for(i=1;i<=M;i++){
            while(tail>head&&Y(q[head+1],j)-Y(q[head],j)<c[i]*(q[head+1]-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-1])<(Y(q[tail],j)-Y(q[tail-1],j))*(i-q[tail])) tail--;
            q[++tail]=i;
        }
    }
    printf("%I64d\n",dp[M][P]);
    return 0;
} 

经验:弹出队首时,可以直接通过比较结果获得。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=100011;
ll d[maxn],c[maxn],sum[maxn],dp[maxn][101],t;
int q[maxn],head,tail;
ll getans(int i,int j,int k){ return dp[k][j-1]+c[i]*(i-k)-(sum[i]-sum[k]); }
ll Y(int k,int j){ return dp[k][j-1]+sum[k]; }
int main()
{
    int N,M,P,i,j,h;
    scanf("%d%d%d",&N,&M,&P);
    for(i=2;i<=N;i++) scanf("%I64d",&d[i]),d[i]+=d[i-1];
    for(i=1;i<=M;i++){
        scanf("%d%I64d",&h,&t);
        c[i]=t-d[h];
    }
    sort(c+1,c+M+1);
    for(i=1;i<=M;i++) sum[i]=sum[i-1]+c[i];
    for(i=1;i<=M;i++) dp[i][1]=c[i]*(i-1)-sum[i-1];
    for(j=2;j<=P;j++){
        head=tail=0;
        for(i=1;i<=M;i++){
            while(tail>head&&getans(i,j,q[head])>getans(i,j,q[head+1])) head++;
            dp[i][j]=getans(i,j,q[head]);
            while(tail>head&&(Y(i,j)-Y(q[tail],j))*(q[tail]-q[tail-1])<(Y(q[tail],j)-Y(q[tail-1],j))*(i-q[tail])) tail--;
            q[++tail]=i; //队首可以getans维护,队尾不行,必须维护斜率!
        }
    }
    printf("%I64d\n",dp[M][P]);
    return 0;
} 

原文地址:https://www.cnblogs.com/hua-dong/p/9246212.html

时间: 2024-10-03 21:41:50

CodeForces - 311B:Cats Transport (DP+斜率优化)的相关文章

幸运飞艇源码下CodeForces - 311B:Cats Transport (DP+斜率优化)

Zxr960115 is owner of a large farm.幸运飞艇源码下载(http://www.1159880099.com ) QQ1159880099 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

(中等) 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 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 fee

学渣乱搞系列之dp斜率优化

学渣乱搞系列之dp斜率优化 By 狂徒归来 貌似dp的斜率优化一直很难搞啊,尤其是像我这种数学很挫的学渣,压根不懂什么凸包,什么上凸下凸的,哎...说多了都是泪,跟wdd讨论了下,得出一些结论.本文很大部分参考了大神Accept的文章,不过此神貌似早已绝迹江湖,这篇文章写得好,也写得很差,前半部分叙述得很好,可是关键,关键部分说得很乱,有些许错误,很多大神都进行了评论指出,但是大神Accept貌似没有修改的意思,故重新总结下,以便自己以后查阅和复习啊. 下面看一个例题Print Article.

BZOJ 1096: [ZJOI2007]仓库建设(DP+斜率优化)

[ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用.突然有一天,L公司的总裁L先生接到气象部门的电话,被告知三天之后将有一场暴雨,于是L先生决定紧急在某些工厂建立一些仓库以免产品被淋坏.由于地形的不同,在不同工厂建立仓库的费用可能是不同的.第i个工厂目前已有成品Pi件,在第i个工厂位置建立仓库的费用是Ci.对于没有建立仓库的工厂,其

【BZOJ-4518】征途 DP + 斜率优化

4518: [Sdoi2016]征途 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 230  Solved: 156[Submit][Status][Discuss] Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜.所以,一段路必须在同一天中走完. Pine希望每一天走的路长度尽可能相近,所以他

Covered Walkway(HDU4258,dp斜率优化)

Covered Walkway Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Your university wants to build a new walkway, and they want at least part of it to be covered. There are certain points which

【BZOJ-1010】玩具装箱toy DP + 斜率优化

1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8432  Solved: 3338[Submit][Status][Discuss] Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具,第i件玩具经过压缩后变成一维长度为Ci.为了方便整理,P

DP 斜率优化题目/决策单调性题目

斜率优化 冬令营讲到了dp斜率优化后瞎写了一些斜率优化,因为毕竟上次写是老早以前了,当时对这个并不是很理解,现在有一点数学基础,稍微好一点了. 瞎找的斜率优化题,可能有点太水了,我这种这么菜的人竟然都切得那么快.可能难度不够,因为有个y坐标不单调要套个平衡树的我是真不会,这个嘛以后再说了. 1.hdu3507 Print Article 给定非负数列,划成若干块,每块价值是左边那玩意儿.求总权值min. 这个自己写写就可以搞出来了吧,现在看真心不难,除非数值有负的..那个要平衡树,现在写不动.

BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][Status][Discuss] Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具,第i件玩具经过压缩后变成一维长度为Ci.为了方便整理,P