题解:洛谷 AT_abc460_c [ABC460C] Sushi

发布时间:2026/6/1 22:39:19

题解:洛谷 AT_abc460_c [ABC460C] Sushi 本文分享的必刷题目是从蓝桥云课、洛谷、AcWing等知名刷题平台精心挑选而来并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。欢迎大家订阅我的专栏算法题解C与Python实现附上汇总贴算法竞赛备考冲刺必刷题C | 汇总【题目来源】洛谷AT_abc460_c [ABC460C] Sushi - 洛谷【题目描述】There areN NNpieces of shari (vinegared rice) andM MMpieces of neta (toppings) as ingredients for sushi.The weight of thei ii-th shari isA i A_iAi​, and the weight of thej jj-th neta isB j B_jBj​.You will make sushi by combining shari and neta.To make one piece of sushi, you need to combine one shari with one neta. Here, the weight of the neta must be at most twice the weight of the shari. Also, the same shari or neta cannot be used in multiple pieces of sushi.Find the maximum number of sushi that can be made.有N NN块舍利醋饭和M MM块寿司配料配料作为寿司的食材。第i ii块舍利的重量为A i A_iAi​第j jj块配料的重量为B j B_jBj​。你需要通过组合舍利和配料来制作寿司。制作一个寿司需要一块舍利和一块配料。这里配料的重量必须不超过舍利重量的两倍。同时同一块舍利或配料不能用于制作多个寿司。求最多能制作的寿司数量。【输入】The input is given from Standard Input in the following format:N NNM MMA 1 A_1A1​A 2 A_2A2​… \ldots…A N A_NAN​B 1 B_1B1​B 2 B_2B2​… \ldots…B M B_MBM​【输出】Output the answer.【输入样例】4 5 4 2 1 8 14 9 3 2 9【输出样例】3【算法标签】#普及- #双指针【代码详解】#includebits/stdc.husingnamespacestd;#defineintlonglongconstintN200005;intn,m,ans;// n: 第一个数组长度m: 第二个数组长度ans: 匹配数量inta[N],b[N];// 两个数组signedmain(){cinnm;// 输入两个数组的长度for(inti1;in;i)// 输入第一个数组cina[i];for(inti1;im;i)// 输入第二个数组cinb[i];sort(a1,an1);// 排序第一个数组sort(b1,bm1);// 排序第二个数组inti1,j1;// 双指针while(injm)// 当两个指针都在数组范围内{if(b[j]2*a[i])// 如果满足条件{i;// 移动第一个数组的指针j;// 移动第二个数组的指针ans;// 匹配数量加1}else// 如果不满足条件{i;// 移动第一个数组的指针}}coutansendl;// 输出匹配数量return0;}【运行结果】4 5 4 2 1 8 14 9 3 2 9 3

相关新闻