news 2026/4/25 0:30:57

题解:AtCoder AT_awc0002_b Fruit Sorting

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
题解:AtCoder AT_awc0002_b Fruit Sorting

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

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

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


【题目来源】

AtCoder:B - 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)(1iN), a sweetness levelA i A_iAihas been measured; the higher this value, the sweeter and more delicious the fruit.
农场收获了N NN个水果,每个水果编号从1 11N NN。对于每个水果i ii1 ≤ i ≤ N 1 ≤ i ≤ N1iN),已测量其甜度水平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 KAiK) 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 ≥ KAiK)的水果被特别视为优质产品,因此即使存在碰伤,也不会被归类为次品,而是包含在常规运输路线中。

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 KK
A 1 A_1A1A 2 A_2A2… \ldotsA N A_NAN
B 1 B_1B1B 2 B_2B2… \ldotsB M B_MBM

  • The 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

【解题思路】

【算法标签】

#模拟#

【代码详解】

#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=200005;intn,m,k;// n: 商品数量,m: 购买商品数量,k: 价格上限inta[N],cnt,ans;// a: 商品价格数组,cnt: 计数,ans: 总价signedmain(){cin>>n>>m>>k;// 读入商品数量,购买商品数量,价格上限for(inti=1;i<=n;i++){cin>>a[i];// 读入每个商品的价格}for(inti=1;i<=m;i++){intx;cin>>x;// 读入要购买的商品编号if(a[x]<k)// 如果商品价格小于上限k{cnt++;// 计数器加1ans+=a[x];// 累加价格}}// 输出:购买的商品数量和总价cout<<cnt<<" "<<ans<<endl;return0;}

【运行结果】

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

MySQL主从数据不一致如何自动修复_利用pt-table-checksum校验

pt-table-checksum连不上从库主因是默认用主库账号连从库且依赖processlist查从库线程&#xff1b;Differences1但pt-table-sync不修复因默认--dry-run&#xff1b;大表慢因CRC计算与chunk划分低效&#xff1b;延迟高时误报因读取旧checksum结果。pt-table-checksum 连不上从库…

作者头像 李华
网站建设 2026/4/25 0:19:40

四博 AI 智能音箱 4G S3 版本技术方案

下面这版更偏技术方案 原型开发说明 可落地代码骨架&#xff0c;适合放到方案书、技术推广文档、客户交流材料中。代码以 ESP-IDF / ESP32-S3 风格写&#xff0c;重点突出四博方案的工程结构、联网切换、远场拾音、实时打断、MCP 扩展、屏幕异显和客户系统接入。四博 AI 智能…

作者头像 李华
网站建设 2026/4/25 0:19:24

四博 AI 拍学机:让孩子开口问,AI 即时答

四博 AI 拍学机&#xff1a;让孩子开口问&#xff0c;AI 即时答孩子学习遇到不会的题&#xff0c;家长没时间辅导&#xff1f; 英语发音不标准&#xff0c;练习缺少互动&#xff1f; 百科知识、课外拓展、口语交流&#xff0c;都希望有一个随时在线的学习伙伴&#xff1f;四博 …

作者头像 李华
网站建设 2026/4/25 0:18:42

实战解析:从理论到代码,梯度提升机(GBM)的核心实现与调优

1. 梯度提升机&#xff08;GBM&#xff09;的本质理解 第一次接触GBM时&#xff0c;我被它"笨鸟先飞"式的训练方式惊艳到了。想象你在教一群学生解题&#xff0c;不是让所有人同时学习&#xff0c;而是让第一个学生先做&#xff0c;记录他犯的错&#xff1b;第二个学…

作者头像 李华