题解:AtCoder AT_awc0087_b Factory Machine Maintenance
本文分享的必刷题目是从蓝桥云课、洛谷、AcWing等知名刷题平台精心挑选而来并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。欢迎大家订阅我的专栏算法题解C与Python实现附上汇总贴算法竞赛备考冲刺必刷题C | 汇总【题目来源】AtCoderB - Factory Machine Maintenance【题目描述】Takahashi is in charge of maintainingN NNmachines at a factory. Takahashi cannot maintain multiple machines simultaneously and must maintain allN NNmachines exactly once each.Maintaining thei ii-th machine takes exactlyT i T_iTiseconds of work time. Additionally, immediately after completing the maintenance of each machine, Takahashi must perform cleaning and servicing of the tools used (hereafter called “tool servicing”). Tool servicing for thei ii-th machine takes exactlyR i R_iRiseconds. The time required for tool servicing is determined by each machine and does not depend on which machine is worked on next. All maintenance and tool servicing must be performed sequentially and cannot be done in parallel. The next machine’s maintenance cannot begin until tool servicing is complete.However, for the last machine to be maintained, since there is no machine to work on afterward, tool servicing can be skipped (and since skipping it results in an earlier completion time, it is always skipped).Travel time between machines is negligible, and the next task can begin immediately after the previous one finishes.That is, if the order of maintenance is( p 1 , p 2 , … , p N ) (p_1, p_2, \ldots, p_N)(p1,p2,…,pN)(a permutation of( 1 , 2 , … , N ) (1, 2, \ldots, N)(1,2,…,N)), the total time required to complete all maintenance is( ∑ k 1 N − 1 ( T p k R p k ) ) T p N \left(\sum_{k1}^{N-1} (T_{p_k} R_{p_k})\right) T_{p_N}(∑k1N−1(TpkRpk))TpNTakahashi can maintain theN NNmachines in any order he likes. If he starts working at time0 00, find the earliest time at which all machine maintenance is completed.高桥负责维护工厂里的N NN台机器。高桥不能同时维护多台机器并且必须恰好维护每台机器一次。维护第i ii台机器需要恰好T i T_iTi秒的工作时间。此外在完成每台机器的维护后高桥必须立即对所使用工具进行清洁和保养以下简称工具保养。第i ii台机器的工具保养需要恰好R i R_iRi秒。工具保养所需时间由每台机器决定与接下来维护哪台机器无关。所有维护工作和工具保养必须顺序进行不能并行。在工具保养完成之前不能开始下一台机器的维护。但是对于最后维护的那台机器由于之后没有机器需要维护可以跳过工具保养因为跳过可以提前完成所以总是跳过。机器之间的移动时间可以忽略不计前一项任务完成后可以立即开始下一项任务。也就是说如果维护顺序为( p 1 , p 2 , … , p N ) (p_1, p_2, \ldots, p_N)(p1,p2,…,pN)( 1 , 2 , … , N ) (1, 2, \ldots, N)(1,2,…,N)的一个排列则完成所有维护所需的总时间为( ∑ k 1 N − 1 ( T p k R p k ) ) T p N \left(\sum_{k1}^{N-1} (T_{p_k} R_{p_k})\right) T_{p_N}(∑k1N−1(TpkRpk))TpN高桥可以按任意顺序维护这N NN台机器。如果他在时间0 00开始工作求完成所有机器维护的最早时间。【输入】N NNT 1 T_1T1R 1 R_1R1T 2 T_2T2R 2 R_2R2⋮ \vdots⋮T N T_NTNR N R_NRNThe first line contains an integerN NNrepresenting the number of machines.Lines2 22throughN 1 N 1N1contain information about each machine.The( 1 i ) (1 i)(1i)-th line contains the work timeT i T_iTirequired to maintain thei ii-th machine and the tool servicing timeR i R_iRirequired after maintaining that machine, separated by a space.【输出】Print the earliest time at which all machine maintenance is completed, on a single line.【输入样例】3 5 3 3 7 8 2【输出样例】21【算法标签】#贪心【代码详解】#includebits/stdc.husingnamespacestd;#defineintlonglongintn,maxr-1,ans;// 任务数量n最大r值maxr总得分anssignedmain()// 主函数{cinn;// 输入任务数量for(inti1;in;i)// 遍历每个任务{intt,r;// 任务的基础时间t额外奖励rcintr;// 输入当前任务的t和ranstr;// 累加每个任务的tr到总分maxrmax(maxr,r);// 更新所有任务中最大的r值}coutans-maxrendl;// 输出总分减去最大r值的结果return0;// 程序正常结束}【运行结果】3 5 3 3 7 8 2 21