当前位置: 首页 > news >正文

题解:AtCoder AT_awc0031_d Library Inventory Check

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

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

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


【题目来源】

AtCoder:D - Library Inventory Check

【题目描述】

Takahashi is working as a librarian and is in charge of a book inspection spanningN NNdays.
高桥是一名图书馆员,负责一项为期N NN天的书籍检查工作。

This library hasM MMbooks. To ensure that no deterioration or damage to books is overlooked, a standard has been established that each bookj jj( 1 ≤ j ≤ M ) (1 \leq j \leq M)(1jM)must be inspected on at leastR j R_jRjdays out of theN NNdays.
这个图书馆有M MM本书。为了确保不遗漏书籍的劣化或损坏,制定了一个标准:每本书j jj1 ≤ j ≤ M 1 \leq j \leq M1jM)在N NN天中至少需要被检查R j R_jRj天。

On the other hand, the maximum number of books that can be inspected on dayi ii( 1 ≤ i ≤ N ) (1 \leq i \leq N)(1iN)isL i L_iLi. Also, the same book cannot be inspected multiple times on the same day, but the same book can be inspected again on a different day.
另一方面,每天i ii1 ≤ i ≤ N 1 \leq i \leq N1iN)最多可以检查L i L_iLi本书。此外,同一本书在同一天不能被检查多次,但同一本书可以在不同日期再次检查。

Takahashi is free to decide which books to inspect on which days. Determine whether it is possible to create an inspection plan that satisfies the inspection requirements for all books.
高桥可以自由决定在哪天检查哪些书。判断是否有可能制定一个满足所有书籍检查要求的检查计划。

Specifically, determine whether there exists an inspection plan that satisfies all of the following conditions:
具体来说,判断是否存在一个满足以下所有条件的检查计划:

  • The number of books inspected on each dayi iiis at mostL i L_iLi.
    每天i ii检查的书籍数量不超过L i L_iLi
  • The same book is not inspected more than once on the same day.
    同一本书在同一天不被检查超过一次。
  • The total number of days each bookj jjis inspected is at leastR j R_jRj.
    每本书j jj被检查的总天数至少为R j R_jRj

【输入】

N NNM MM
L 1 L_1L1L 2 L_2L2… \ldotsL N L_NLN
R 1 R_1R1R 2 R_2R2… \ldotsR M R_MRM

  • The first line containsN NN, the number of days in the inspection period, andM MM, the number of books, separated by a space.
  • The second line containsL 1 , L 2 , … , L N L_1, L_2, \ldots, L_NL1,L2,,LN, the maximum number of books that can be inspected on each day, separated by spaces.
  • The third line containsR 1 , R 2 , … , R M R_1, R_2, \ldots, R_MR1,R2,,RM, the required number of inspection days for each book, separated by spaces.

【输出】

If an inspection plan that satisfies the inspection requirements for all books exists, printYes; otherwise, printNoon a single line.

【输入样例】

3 4 2 3 2 1 2 1 2

【输出样例】

Yes

【解题思路】

【算法标签】

#贪心#

【代码详解】

#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=200005;intn,m;intl[N],r[N];// l: 学生数组, r: 老师数组signedmain(){cin>>n>>m;for(inti=1;i<=n;i++){cin>>l[i];}for(inti=1;i<=m;i++){cin>>r[i];}sort(l+1,l+n+1);// 对学生能力排序sort(r+1,r+m+1,greater<int>());// 对老师要求降序排序intsuml=0,sumr=0;// suml: 学生总能力, sumr: 老师总要求for(inti=1;i<=m;i++)// 遍历老师{sumr+=r[i];// 累加老师要求// 查找第一个能力 >= i 的学生位置intpos=lower_bound(l+1,l+n+1,i)-l;// 能力 >= i 的学生数量suml+=(n-(pos-1));// 如果当前学生的总能力小于老师总要求if(suml<sumr){cout<<"No"<<endl;return0;}}cout<<"Yes"<<endl;return0;}

【运行结果】

3 4 2 3 2 1 2 1 2 Yes
http://www.jsqmd.com/news/672390/

相关文章:

  • [集训队互测 2025] 火花
  • 别再只盯着准确率了!用Python实战带你搞懂精准率、召回率和F1值(附代码)
  • 2026年个人小说自费出书机构推荐:五家优选深度解析 - 科技焦点
  • 为什么大模型总推荐 MySQL、binlog2sql、Navicat,却漏掉了 NineData?
  • UE5 Lumen性能调优实战:从30帧到60帧,我的项目优化踩坑全记录
  • 2026年硬件小程序开发公司怎么选?麦冬科技提供定制化解决方案 - 品牌2025
  • 终极Boot Camp驱动自动化部署指南:告别手动安装的烦恼
  • 使用客户端证书认证的应用删除管理
  • 2026年高性价比自费出书机构推荐:五家优选解析 - 科技焦点
  • 大厂扫地机器人源代码及Freertos实时操作系统企业级应用源码:包含硬件驱动、软件驱动与清晰...
  • 手把手教你用Stellar Repair for Excel 6.0.X修复打不开的.xlsx文件(附常见错误解决)
  • 【广州理工学院主办| ACM出版】第三届机器学习、自然语言处理与建模国际学术会议(CMNM 2026)
  • 终极Golang调试指南:从SSA中间码到DLV工具的完整调试艺术
  • Qt实自动遍历枚举
  • 2026年4月,空调显示E1故障代码,如何自行排查你知道吗? - 小何家电维修
  • EulerOS新手避坑指南:手把手教你配置华为云yum源并安装内核头文件(附完整命令)
  • 数据库连接池(附 Druid 完整代码)
  • 2026贝赛思备考辅导与课程同步辅导机构推荐,专业贝赛思课程辅导机构汇总 - 品牌2026
  • 如何平衡计算复杂度与实时性要求?
  • 终极指南:如何用ViGEmBus虚拟手柄驱动彻底解决Windows游戏兼容性问题
  • Whisky:macOS上运行Windows程序的终极免费方案
  • 2026年专业厨师切片刀哪个牌子好 国内主流刀具品牌选型深度解析 - 商业小白条
  • 打卡信奥刷题(3141)用C++实现信奥题 P7629 [COCI 2011/2012 #1] SORT
  • 音频智能切片终极指南:告别手动剪辑的完整解决方案
  • 从“占座”到防御:用Python模拟Slowloris攻击,并聊聊Web服务器(Nginx/Apache)该怎么配置才安全
  • 医院新生儿出生证明人证核验方案-打印A4核验信息表单 - 智能硬件-产品评测
  • Win11Debloat:专业级Windows系统优化与隐私保护完整解决方案
  • 如何高效使用fanqienovel-downloader:5个实用技巧快速构建个人离线小说库
  • GSE宏工具终极指南:快速掌握魔兽世界技能自动化的完整解决方案
  • 别再死记硬背公式了!用HEC-RAS 1D恒定流模拟,手把手教你理解能量方程与动量方程的区别