题解:AtCoder AT_awc0002_b Fruit Sorting
本文分享的必刷题目是从蓝桥云课、洛谷、AcWing等知名刷题平台精心挑选而来并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。欢迎大家订阅我的专栏算法题解C与Python实现附上汇总贴算法竞赛备考冲刺必刷题C | 汇总【题目来源】AtCoderB - Fruit Sorting (atcoder.jp)【题目描述】Takahashi is in charge of shipping fruits harvested at a farm.高桥负责运输农场收获的水果。N NNfruits have been harvested at the farm, each numbered from1 11toN NN. For each fruiti ii( 1 ≤ i ≤ N ) (1 \leq i \leq N)(1≤i≤N), a sweetness levelA i A_iAihas been measured; the higher this value, the sweeter and more delicious the fruit.农场收获了N NN个水果每个水果编号从1 11到N NN。对于每个水果i ii1 ≤ i ≤ N 1 ≤ i ≤ N1≤i≤N已测量其甜度水平A i A_iAi该值越高水果越甜、越美味。It was discovered thatM MMfruits have bruises. The numbers of the bruised fruits are given asB 1 , B 2 , … , B M B_1, B_2, \ldots, B_MB1,B2,…,BM(the order has no special meaning).发现有M MM个水果存在碰伤。碰伤水果的编号依次为B 1 , B 2 , … , B M B_1, B_2, …, B_MB1,B2,…,BM顺序无特殊含义。Normally, bruised fruits are shipped as substandard products through a separate route from the regular one. However, fruits with a sweetness level ofK KKor higher (i.e.,A i ≥ K A_i \geq KAi≥K) are treated specially as premium products, so even if they have bruises, they are not classified as substandard and are included in the regular shipping route.通常碰伤水果会通过独立于常规路线的特殊路径作为次品运输。然而甜度水平为K KK或以上即A i ≥ K A_i ≥ KAi≥K的水果被特别视为优质产品因此即使存在碰伤也不会被归类为次品而是包含在常规运输路线中。In summary, fruits thathave bruises and have a sweetness level less thanK KKare shipped as substandard products.总之存在碰伤且甜度水平小于K KK的水果将作为次品运输。Find the number of fruits that Takahashi should ultimately ship as substandard products, and the total sweetness level of those fruits.求高桥最终应作为次品运输的水果数量以及这些水果的总甜度水平。【输入】N NNM MMK KKA 1 A_1A1A 2 A_2A2… \ldots…A N A_NANB 1 B_1B1B 2 B_2B2… \ldots…B M B_MBMThe first line containsN NNrepresenting the total number of fruits,M MMrepresenting the number of bruised fruits, andK KKrepresenting the sweetness threshold for premium classification, separated by spaces.The second line contains the sweetness levels of each fruitA 1 , A 2 , … , A N A_1, A_2, \ldots, A_NA1,A2,…,AN, separated by spaces.A i A_iAirepresents the sweetness level of fruiti ii.The third line contains the numbers of the bruised fruitsB 1 , B 2 , … , B M B_1, B_2, \ldots, B_MB1,B2,…,BM, separated by spaces.B j B_jBjrepresents the number of a bruised fruit.【输出】Print the number of fruits to be shipped as substandard products and the total sweetness level of those fruits, separated by a space, on a single line.【输入样例】5 3 10 8 12 5 15 7 1 3 4【输出样例】2 13【解题思路】【算法标签】#模拟#【代码详解】#includebits/stdc.husingnamespacestd;#defineintlonglongconstintN200005;intn,m,k;// n: 商品数量m: 购买商品数量k: 价格上限inta[N],cnt,ans;// a: 商品价格数组cnt: 计数ans: 总价signedmain(){cinnmk;// 读入商品数量购买商品数量价格上限for(inti1;in;i){cina[i];// 读入每个商品的价格}for(inti1;im;i){intx;cinx;// 读入要购买的商品编号if(a[x]k)// 如果商品价格小于上限k{cnt;// 计数器加1ansa[x];// 累加价格}}// 输出购买的商品数量和总价coutcnt ansendl;return0;}【运行结果】5 3 10 8 12 5 15 7 1 3 4 2 13