Codeforces Round #185 (Div. 2),D. Cats Transport

 

解题报告:Codeforces Round #185 (Div. 2),D. Cats Transport

http://codeforces.com/contest/312/problem/D

题目描述:

D. Cats Transport

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 dimeters. 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 cincoutstreams or the %I64d specifier.

Sample test(s)

input

4 6 2
1 3 5
1 0
2 1
4 9
1 10
2 10
3 12

output

3

 

思路:

对于样例:

 

问题转换:

对于一个已经排好的序列{0,0,0,8,9,10},和给定的p{p=2},如何划分子集,才可以使得满足条件的花费最小。

考察算法:单调队列+dp

 

复杂度有点大,对递推式需要进行优化,

 

单调队列,维护下凸线!!!

如果存在f(i)+f(i)>f(i-1)+f(i+1),那么不论斜率a[i]是处于什么范围,f(i),都不会比f(i-1),f(i+1)更优,如是,删除f(i),维护一个下凸队列

 

另外,如果i-1k’转移,ik转移,则有k>=k’,原因是切线的斜率a[i]是递增的

 

 

代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
typedef __int64 Uint;
#define N 100050

Uint dp[110][N];
Uint a[N];
Uint sum[N];
int q[N];
int d[N];
int main()
{
    int n,m,p;
    int x,y;
    scanf("%d%d%d",&n,&m,&p);
    d[1] = 0;
    for (int i = 2 ; i <= n ; d[i] += d[i-1],i++)
        scanf("%d",&d[i]);
    for (int i = 1 ; i <= m ; i++)
    {
        scanf("%d%d",&x,&y);
        a[i] = y-d[x];
    }
    sort(a+1,a+m+1);

    sum[0] = 0;
    for (int i = 1 ; i <= m ; i++)
        sum[i] = sum[i-1] + a[i];
    memset(dp,0,sizeof(dp));
    for (int i = 1 ; i <= m ; i++)
        dp[1][i] = i * a[i] - sum[i];
    for (int i = 2 ; i <= p ; i++)
    {
        q[1] = 0;
        int base = 1;
        int top = 1;
        for (int j = 1 ; j <= m ; j++)
        {
            while ((base < top) && ((dp[i-1][q[base+1]]+sum[q[base+1]]) - (dp[i-1][q[base]]+sum[q[base]]) < (q[base+1] - q[base]) * a[j])) base++;
            dp[i][j] = dp[i-1][q[base]] + (j-q[base])*a[j] - (sum[j]-sum[q[base]]);
            q[++top] = j;
            while (base < top-1 && ((dp[i-1][q[top]]+sum[q[top]])-(dp[i-1][q[top-1]]+sum[q[top-1]]))*(q[top-1]-q[top-2]) < ((dp[i-1][q[top-1]]+sum[q[top-1]])-(dp[i-1][q[top-2]]+sum[q[top-2]]))*(q[top]-q[top-1]))
            {
                top--;
                q[top] = q[top+1];
            }
        }
    }
    printf("%I64d\n",dp[p][m]);
    return 0;
}
/*
4 6 2
1 3 5
1 0
2 1
4 9
1 10
2 10
3 12

*/


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值