Postman Gym - 101201I

探讨在一维世界中,邮递员如何高效地完成所有信件的递送任务,并返回邮局,重点在于通过贪心算法确定最优路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值