news 2026/6/12 0:23:00

题解:AtCoder AT_awc0083_c Laying Optical Fiber

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
题解:AtCoder AT_awc0083_c Laying Optical Fiber

本文分享的必刷题目是从蓝桥云课洛谷AcWing等知名刷题平台精心挑选而来,并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构,旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。

欢迎大家订阅我的专栏:算法题解:C++与Python实现!

附上汇总贴:算法竞赛备考冲刺必刷题(C++) | 汇总


【题目来源】

AtCoder:C - Laying Optical Fiber

【题目描述】

Takahashi is an engineer at a telecommunications company. He has been assigned a project to lay new fiber optic cables.

There areN NNrelay stations along a road, and each station is aligned in a straight line. The stations are numbered1 , 2 , … , N 1, 2, \ldots, N1,2,,Nin order of increasing distance from the starting point of the road, and thei ii-th station is locatedX i X_iXimeters from the starting point (X 1 < X 2 < ⋯ < X N X_1 < X_2 < \cdots < X_NX1<X2<<XN). Activating each stationi iicostsC i C_iCiten-thousand yen.

Takahashi’s project has been allocated a budget ofM MMten-thousand yen. To lay the fiber optic cable, Takahashi can select one contiguous interval of stations. Specifically, he chooses a pair of integers( l , r ) (l, r)(l,r)satisfying1 ≤ l ≤ r ≤ N 1 \leq l \leq r \leq N1lrN, and activates all stations from thel ll-th to ther rr-th. The required cost is the total activation cost of all stations in the chosen interval,∑ i = l r C i \displaystyle\sum_{i=l}^{r} C_ii=lrCiten-thousand yen, which must not exceed the budget ofM MMten-thousand yen. Note that the cost of laying cables between stations can be ignored.

When a valid( l , r ) (l, r)(l,r)is chosen under this condition, a fiber optic cable ofX r − X l X_r - X_lXrXlmeters can be laid (ifl = r l = rl=r, the length is0 00meters). If there is no interval that fits within the budget, or if Takahashi chooses not to select any interval, nothing is laid, and the cable length is0 00meters.

Find the maximum length of fiber optic cable that Takahashi can lay, in meters.

高桥是一家电信公司的工程师。他被分配了一个铺设新光纤电缆的项目。

沿着一条道路有N NN个中继站,每个站点在一条直线上排列。站点按照距离道路起点的距离递增顺序编号为1 , 2 , … , N 1, 2, \ldots, N1,2,,N,第i ii个站点位于距离起点X i X_iXi米处(X 1 < X 2 < ⋯ < X N X_1 < X_2 < \cdots < X_NX1<X2<<XN)。激活每个站点i ii的成本为C i C_iCi万日元。

高桥的项目被分配了M MM万日元的预算。为了铺设光纤电缆,高桥可以选择一个连续的站点区间。具体来说,他选择一对满足1 ≤ l ≤ r ≤ N 1 \leq l \leq r \leq N1lrN的整数( l , r ) (l, r)(l,r),并激活从第l ll个到第r rr个的所有站点。所需成本是所选区间内所有站点的总激活成本,即∑ i = l r C i \displaystyle\sum_{i=l}^{r} C_ii=lrCi万日元,这个成本不能超过预算M MM万日元。注意,站点之间铺设电缆的成本可以忽略不计。

当在此条件下选择一个有效的( l , r ) (l, r)(l,r)时,可以铺设长度为X r − X l X_r - X_lXrXl米的光纤电缆(如果l = r l = rl=r,则长度为0 00米)。如果没有符合预算的区间,或者高桥选择不选任何区间,则不铺设任何电缆,电缆长度为0 00米。

求高桥可以铺设的光纤电缆的最大长度,单位为米。

【输入】

N NNM MM
X 1 X_1X1C 1 C_1C1
X 2 X_2X2C 2 C_2C2
⋮ \vdots
X N X_NXNC N C_NCN

  • The first line contains an integerN NNrepresenting the number of stations and an integerM MMrepresenting the budget (in ten-thousand yen), separated by a space.
  • From the 2nd line to the( N + 1 ) (N + 1)(N+1)-th line, information about each station is given.
  • The( 1 + i ) (1 + i)(1+i)-th line contains an integerX i X_iXirepresenting the position (in meters) of thei ii-th station and an integerC i C_iCirepresenting the cost (in ten-thousand yen) required to activate that station, separated by a space.
  • The stations are given in ascending order of position, and it is guaranteed thatX 1 < X 2 < ⋯ < X N X_1 < X_2 < \cdots < X_NX1<X2<<XN.

【输出】

Print the maximum length of fiber optic cable that Takahashi can lay, in meters, on a single line.

【输入样例】

5 10 1 2 3 3 7 4 12 5 20 6

【输出样例】

6

【核心思想】

  1. 问题分析:给定N NN个按位置升序排列的中继站,每个站点有位置X i X_iXi和激活成本C i C_iCi。需要选择一个连续区间[ l , r ] [l, r][l,r],使得区间内总成本∑ i = l r C i ≤ M \sum_{i=l}^{r} C_i \leq Mi=lrCiM,同时最大化区间两端的位置差X r − X l X_r - X_lXrXl。这是一个**双指针(滑动窗口)**问题,关键在于如何在成本约束下找到最优区间。

  2. 算法选择

    • 双指针(滑动窗口):使用两个指针l ll(左指针)和r rr(右指针)维护一个窗口[ l , r ] [l, r][l,r],保证窗口内总成本不超过M MM
    • 单调性利用:由于站点按位置升序排列,X r − X l X_r - X_lXrXl随窗口扩大而增大,但成本约束限制了窗口大小
  3. 关键步骤

    • 初始化l = 1 l = 1l=1(左指针),s u m = 0 sum = 0sum=0(当前窗口总成本),m a x n = 0 maxn = 0maxn=0(最大位置差)
    • 扩展窗口(右指针r rr1 11N NN):
      • 将第r rr个站点的成本加入窗口:sum += c[r]
    • 收缩窗口:当sum > M时(成本超限)
      • 不断移除左指针站点的成本:sum -= c[l]
      • 左指针右移:l++
    • 更新答案:如果窗口有效(l <= rsum <= M
      • 计算当前位置差:x[r] - x[l]
      • 更新最大位置差:maxn = max(maxn, x[r] - x[l])
    • 输出最大位置差m a x n maxnmaxn
  4. 时间/空间复杂度

    • 时间复杂度:O ( N ) O(N)O(N),每个站点最多被左右指针各访问一次
    • 空间复杂度:O ( N ) O(N)O(N),存储站点位置和成本
  5. 双指针(滑动窗口)的核心思想

    • 单调性:右指针r rr只向右移动,左指针l ll也只向右移动,不会回退
    • 窗口维护:始终保持窗口[ l , r ] [l, r][l,r]内的总成本不超过预算M MM
    • 贪心策略:对于每个右端点r rr,找到最远的左端点l ll使得区间[ l , r ] [l, r][l,r]的成本不超过M MM,此时X r − X l X_r - X_lXrXl是以r rr结尾的最大位置差
    • 线性扫描:利用双指针的单调性,将暴力枚举的O ( N 2 ) O(N^2)O(N2)优化到O ( N ) O(N)O(N)
    • 适用于连续子数组满足某约束条件下的最值问题

【算法标签】

#双指针

【代码详解】

#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=200005;// 定义数组最大长度intn,m,maxn,sum;// 物品数量n,重量限制m,最大差值maxn,当前重量和sumintx[N],c[N];// 位置数组x,重量数组csignedmain(){cin>>n>>m;// 输入物品数量和重量限制for(inti=1;i<=n;i++)// 遍历读取所有物品cin>>x[i]>>c[i];// 输入物品位置和重量intl=1;// 初始化滑动窗口左指针for(intr=1;r<=n;r++)// 滑动窗口右指针遍历{sum+=c[r];// 将右指针物品加入当前重量while(sum>m&&l<=r)// 如果当前总重量超过限制{sum-=c[l];// 移除左指针物品l++;// 左指针右移}if(l<=r&&sum<=m)// 如果窗口有效且重量不超限{maxn=max(maxn,x[r]-x[l]);// 更新位置最大差值}}cout<<maxn<<endl;// 输出最大差值return0;// 程序正常结束}

【运行结果】

5 10 1 2 3 3 7 4 12 5 20 6 6
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/12 0:19:59

PDO查数据库大表不会出现内存溢出?

答案是&#xff1a;默认情况下&#xff0c;一定会溢出&#xff1b;但如果使用正确的姿势&#xff08;游标/生成器&#xff09;&#xff0c;可以避免溢出。 它的本质是&#xff1a;**PDO 本身只是一个 数据库抽象层 (Database Abstraction Layer)&#xff0c;它不决定数据怎么存…

作者头像 李华
网站建设 2026/6/12 0:19:57

坏消息更需要及时回音。

它的本质是&#xff1a;**坏消息的回音不是“道歉”&#xff0c;而是一次 紧急的状态同步 (Emergency State Sync) 和 风险共担邀请 (Risk Sharing Invitation)。 核心矛盾&#xff1a;人性本能倾向于回避痛苦和冲突&#xff08;鸵鸟心态&#xff09;。我们害怕面对愤怒的老板、…

作者头像 李华
网站建设 2026/6/12 0:15:02

如何彻底重置Navicat Premium试用期:macOS版终极解决方案指南

如何彻底重置Navicat Premium试用期&#xff1a;macOS版终极解决方案指南 【免费下载链接】navicat_reset_mac navicat mac版无限重置试用期脚本 Navicat Mac Version Unlimited Trial Reset Script 项目地址: https://gitcode.com/gh_mirrors/na/navicat_reset_mac 还在…

作者头像 李华
网站建设 2026/6/12 0:15:00

终极指南:如何免费快速地将OFD文件转换为PDF格式

终极指南&#xff1a;如何免费快速地将OFD文件转换为PDF格式 【免费下载链接】Ofd2Pdf Convert OFD files to PDF files. 项目地址: https://gitcode.com/gh_mirrors/ofd/Ofd2Pdf 你是否经常收到OFD格式的电子发票、政府公文或电子证照&#xff0c;却苦于找不到合适的阅…

作者头像 李华
网站建设 2026/6/12 0:14:08

Adobe-GenP 3.0:解锁Adobe创意云的全能补丁解决方案

Adobe-GenP 3.0&#xff1a;解锁Adobe创意云的全能补丁解决方案 【免费下载链接】Adobe-GenP Adobe CC 2019/2020/2021/2022/2023 GenP Universal Patch 3.0 项目地址: https://gitcode.com/gh_mirrors/ad/Adobe-GenP Adobe-GenP 3.0是一款革命性的Adobe Creative Cloud…

作者头像 李华
网站建设 2026/6/12 0:14:07

COM3D2.MaidFiddler:3分钟掌握COM3D2女仆实时编辑器的完整指南

COM3D2.MaidFiddler&#xff1a;3分钟掌握COM3D2女仆实时编辑器的完整指南 【免费下载链接】COM3D2.MaidFiddler Maid Fiddler for COM3D2 -- a real-time value editor for COM3D2 项目地址: https://gitcode.com/gh_mirrors/co/COM3D2.MaidFiddler 想象一下&#xff0…

作者头像 李华