本文分享的必刷题目是从蓝桥云课、洛谷、AcWing等知名刷题平台精心挑选而来并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。欢迎大家订阅我的专栏算法题解C与Python实现附上汇总贴算法竞赛备考冲刺必刷题C | 汇总【题目来源】AtCoderD - Returning Library Books【题目描述】Takahashi works as a librarian at a library. Today, he must returnN NNbooks that were returned by patrons to their correct positions on the shelves.The library is on a number line, and the counter is located at coordinate0 00. The shelf where thei ii-th book( 1 ≤ i ≤ N ) (1 \leq i \leq N)(1≤i≤N)should be placed is at coordinateX i X_iXi​. Shelves can exist in both the positive and negative directions from the counter. Different books may need to be returned to the same coordinate.Takahashi starts at the counter (coordinate0 00). He can carry at mostK KKbooks at a time. He repeats the following procedure until all books have been returned:At the counter (coordinate0 00), choose between1 11andK KKbooks (inclusive) from among the books not yet returned, and take them out.Move freely along the number line, and drop off each book when reaching the coordinate of its corresponding shelf. The order in which shelves are visited is free to choose. It is allowed to visit shelves in both the positive and negative directions within the same trip.After all books taken out have been placed on their correct shelves, return to the counter (coordinate0 00). This completes one round trip.All books taken out must be placed on their correct shelves during that round trip. Books cannot be brought back to the counter to be carried on a later trip. After every round trip, including the last one, Takahashi must return to the counter.The travel distance of one round trip is the total distance actually traveled along the number line during that trip. For example, if Takahashi moves from coordinate0 00to coordinate5 55, then from there to coordinate− 3 -3−3, and then back to coordinate0 00, the travel distance is5 8 3 16 5 8 3 1658316.The sum of travel distances across all round trips is called thetotal travel distance. Find the minimum total travel distance to return all books to their correct shelves and return to the counter.高桥在一家图书馆担任图书管理员。今天他需要将读者归还的N NN本书放回书架上正确的位置。图书馆在一条数轴上柜台位于坐标0 00处。第i ii本书1 ≤ i ≤ N 1 \leq i \leq N1≤i≤N应放置的书架位于坐标X i X_iXi​处。书架可以存在于柜台的正方向和负方向。不同的书可能需要归还到相同的坐标。高桥从柜台坐标0 00出发。他一次最多可以携带K KK本书。他重复以下步骤直到所有书都被归还在柜台坐标0 00处从尚未归还的书中选择1 11到K KK本书含端点并取出。沿数轴自由移动并在到达相应书架坐标时放下每本书。访问书架的顺序可以自由选择。在同一次行程中可以同时访问正方向和负方向的书架。将所有取出的书放到正确的书架上后返回柜台坐标0 00。这完成了一次往返行程。所有取出的书必须在该次往返行程中放到正确的书架上。不能将书带回柜台留待后续行程携带。每次往返行程后包括最后一次高桥都必须返回柜台。一次往返行程的移动距离是该行程中沿数轴实际移动的总距离。例如如果高桥从坐标0 00移动到坐标5 55然后从那里移动到坐标− 3 -3−3再返回坐标0 00则移动距离为5 8 3 16 5 8 3 1658316。所有往返行程的移动距离之和称为总移动距离。求将所有书放回正确书架并返回柜台所需的最小总移动距离。【输入】N NNK KKX 1 X_1X1​X 2 X_2X2​… \ldots…X N X_NXN​The first line contains an integerN NNrepresenting the number of books and an integerK KKrepresenting the maximum number of books that can be carried at once, separated by a space.The second line contains integersX 1 , X 2 , … , X N X_1, X_2, \ldots, X_NX1​,X2​,…,XN​representing the coordinates of the shelves where each book should be returned, separated by spaces.【输出】Print the minimum total travel distance to return all books and come back to the counter, as a single integer on one line.【输入样例】3 2 1 3 5【输出样例】12【算法标签】#贪心【代码详解】#includebits/stdc.husingnamespacestd;#defineintlonglongconstintN200005;// 定义最大数组长度intn,k,ans;// 物品数量n每次搬运数量k总路程ansintpos[N],neg[N],cur1,cur2;// 正数数组pos负数数组neg各自计数器signedmain()// 主函数{cinnk;// 输入物品数量和每次搬运数量for(inti1;in;i)// 读取所有物品位置{intx;cinx;// 输入物品位置if(x0)// 如果是正数或零pos[cur1]x;// 存入正数数组else// 如果是负数neg[cur2]x;// 存入负数数组}sort(pos1,poscur11,greaterint());// 正数从大到小排序sort(neg1,negcur21);// 负数从小到大排序for(inti1;icur1;ik)// 每次取k个最远的正数anspos[i]*2;// 来回路程加倍for(inti1;icur2;ik)// 每次取k个最远的负数ansabs(neg[i])*2;// 取绝对值来回路程加倍coutansendl;// 输出总路程return0;// 程序正常结束}【运行结果】