Cats Transport Codeforces 311B 斜率优化

题意是(我语文不好)
n只小猫在不同的时间去爬m座山,下不来了,需要p个饲养员去帮它们。
n,m<=100,000;p<=100;
山在一条线上,给出他们之间的距离,以及每只猫在山上的时间。
饲养员从1号山出发,速度为1,可以在任意时间出发。
只能猫等人,人不会等猫;
求所有猫最小的等待时间之和。

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 2
1 3 5
1 0
2 1
4 9
1 10
2 10
3 12
output
3

把每只猫的到达山上的时间减去一号山与这座山的距离,就可以把每一只猫对应在原点上,可以证明(显然)每一个饲养员帮助的猫是原点上连续的一段,这一段猫的等待时间就是 最大的时间减去每一只猫的时间。问题就变成了把一个序列分成p段所产生的价值最小,经典的斜率优化题目。
dp[k][p]=dp[i][p-1]+时间[k]*(k-i)-Σ时间[i~k];

#include<algorithm>
#include<cstdio>
#include<iostream>
#define LL long long
using namespace std;
LL  d[100005];
LL  c[100005];
LL  s[100005];
LL dp[100005][105];
int q[100005];
double get(int i,int j,int k)
{
    double ans;
    ans=(double)(dp[i][k-1]-dp[j][k-1]+s[i]-s[j]);
    ans/=(double)(j-i);
    return ans;
}
int main()
{
    int n,m,p;
    scanf("%d%d%d",&n,&m,&p);
    for(int i=2;i<=n;i++)
    {
        scanf("%I64d",&d[i]);
        d[i]+=d[i-1];
    }
    LL x,y;
    for(int i=1;i<=m;i++)
    {
        scanf("%I64d%I64d",&x,&y);
        c[i]=y-d[x];
    }
    sort(c+1,c+m+1);
    for(int i=1;i<=m;i++)
        s[i]=s[i-1]+c[i];
    for(int i=1;i<=m;i++)
        dp[i][1]=c[i]*(i-1)-s[i-1];
    for(int j=2;j<=p;j++)
    {
        int head=0,tail=0,w;
        for(int i=1;i<=m;i++)
        {
            while(head<tail&&get(q[head],q[head+1],j)>(double)(-c[i]))
                head++;
            w=q[head];
            dp[i][j]=dp[w][j-1]+c[i]*(i-w)-s[i]+s[w];
            while(head<tail&&get(q[tail-1],q[tail],j)<get(q[tail],i,j))
                tail--;
            q[++tail]=i;
        }
    }
    printf("%I64d",dp[m][p]);
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值