Postman Gym - 101201I

版权声明:本文为博主原创文章,未经博主允许不得转载。若有误,欢迎私信指教~ https://blog.csdn.net/little_starfish/article/details/80475890

Describe

A postman delivers letters to his neighbors in a one-dimensional world.
The post office, which contains all of the letters to begin with, is located at x = 0, and there are
n houses to which the postman needs to deliver the letters. House i is located at position xi, and there are mi letters that need to be delivered to this location. But the postman can only carry k letters at once.
The postman must start at the post office, pick up some number of letters less than or equal to his carrying capacity, and then travel to some of the houses dropping off letters. He must then return to the post office, repeating this process until all letters are delivered. At the end he must return to the post office.
The postman can travel one unit of distance in one unit of time.
What is the minimum amount of time it will take the postman to start at the post office, deliver all the letters, and return to the post office?

Input

The first line of input contains two space-separated integers n (1 ≤ n ≤ 1,000) and k (1 ≤ k ≤ 1e7).
Each of the next n lines contains two space-separated integers xi (|xi| ≤ 1e7) and mi (1 ≤ mi ≤ 1e7).

Output

Print, on a single line, the minimum amount of time it will take to complete the mail delivery route.

题意

一个邮递员在一个一维的数轴上送邮件,邮局坐标0,每次邮递员最多可以从邮局装K封信,给出N个需要投递的信件的坐标和需要投递的信件数量(N较小,其余数据大,需要用long long int),问快递员需要走的最短距离(最后需要回到邮局)。

分析

简单贪心,每次到最远距离投递邮件,然后往邮局走,如果投递完一个地点的邮件后,若邮递员手上有剩余的邮件则可以继续投递,这样就能保证路程最短,当然某个地点可能需要投很多邮件,一趟不够,这样就返回邮局拿K封信,再去就行(需要直接计算对于单个地点需要跑多少趟,否则会超时,具体细节见代码)。我是把坐标正和负分开算,排序。

Sample Input

4 10
-7 5
-2 3
5 7
9 5

Sample Output

42

Sample Input

7 1
9400000 10000000
9500000 10000000
9600000 10000000
9700000 10000000
9800000 10000000
9900000 10000000
10000000 10000000

Sample Output

1358000000000000

AC代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
typedef struct node
{
    ll x;  //坐标
    ll num;  //需要投递的信件数目
}node;
node a[1111], b[1111];
//按照距离邮局的绝对距离从大到小排序
bool cmp1(node a, node b){  
    return a.x < b.x;
}
bool cmp2(node a, node b){
    return a.x > b.x;
}
int main()
{
    int i, n, numa = 0, numb = 0;
    ll k, xx, yy;
    ll ans = 0;
    scanf("%d %lld", &n, &k);
    for(i = 0; i < n; i++){
        scanf("%lld %lld", &xx, &yy);
        if(xx < 0){
            numa++;
            a[numa].x = xx;
            a[numa].num = yy;
        }
        else{
            numb++;
            b[numb].x = xx;
            b[numb].num = yy;
        }
    }
    numa++;  //多存一个邮局,方便计算距离
    a[numa].x = 0;
    a[numa].num = 0;
    numb++;
    b[numb].x = 0;
    b[numb].num = 0;
    sort(a+1,a+1+numa,cmp1);  //排序
    sort(b+1,b+1+numb,cmp2);
    ll d = k;  //d为当前邮递员手上的邮件数目
    ll m;  //需要来回的趟数
    ans -= a[1].x;  //先去左边的最远地方
    for(i = 1; i < numa; i++){
        if(d >= a[i].num){  //如果当前的邮件 > 需要投递的数目
            d -= a[i].num;
            a[i].num = 0;
            ans += (a[i+1].x-a[i].x);  //+到下一个地点走的长度
        }
        else{
            a[i].num -= d;
            if(a[i].num % k == 0){  整除
                d = 0;  //到下个地点的前,d=0
                m = a[i].num / k;  //需要跑的趟数
                ans -= (m * 2 * a[i].x);  //来回的总距离
                ans += (a[i+1].x-a[i].x);  //+到下一个地点走的长度
            }
            else{  //非整除
                m = (a[i].num-a[i].num%k)/k;
                m++;
                d = k - a[i].num%k;
                ans -= (m * 2 * a[i].x);
                ans += (a[i+1].x-a[i].x);
            }
        }
    }
    //走右边,与左边同理
    d = k;
    ans += b[1].x;
    for(i = 1; i < numb; i++){
        if(d >= b[i].num){
            d -= b[i].num;
            b[i].num = 0;
            ans += (b[i].x-b[i+1].x);
        }
        else{
            b[i].num -= d;
            if(b[i].num % k == 0){
                d = 0;
                m = b[i].num / k;
                ans += (m * 2 * b[i].x);
                ans += (b[i].x-b[i+1].x);
            }
            else{
                m = (b[i].num-b[i].num%k)/k;
                m++;
                d = k - b[i].num%k;
                ans += (m * 2 * b[i].x);
                ans += (b[i].x-b[i+1].x);
            }
        }
    }
    printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/little_starfish/article/details/80475890