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

题解:AtCoder AT_awc0003_c Bargain Sale Selection

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

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

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


【题目来源】

AtCoder:C - Bargain Sale Selection

【题目描述】

Takahashi will purchase allN NNitems, one of each.
高桥将购买全部N NN件商品,每样一件。

Each item has two prices: a “regular price” and a “sale price.” The regular price of itemi ii( 1 ≤ i ≤ N ) (1 \leq i \leq N)(1iN)isA i A_iAiyen, and the sale price isB i B_iBiyen. It is guaranteed that the sale price is at most the regular price, that is,B i ≤ A i B_i \leq A_iBiAi.
每件商品有两种价格:“原价"和"促销价”。商品i ii1 ≤ i ≤ N 1 ≤ i ≤ N1iN)的原价为A i A_iAi日元,促销价为B i B_iBi日元。保证促销价不超过原价,即B i ≤ A i B_i ≤ A_iBiAi

Takahashi has one special discount coupon. With this coupon, he can chooseat mostK KKitems from theN NNitems and purchase the chosen items at their sale prices. The remaining items that are not chosen will be purchased at their regular prices. Note that the same item cannot be chosen more than once, and it is also allowed to choose no items (i.e., choose0 00items).
高桥有一张特殊折扣券。使用此券,他可以从N NN件商品中最多选择K KK件,并以促销价购买选中的商品。未选中的剩余商品将按原价购买。注意,同一件商品不能被选择超过一次,并且也可以选择0 00件商品(即选择0 00件)。

When Takahashi optimally chooses which items to purchase at the sale price, find the minimum possible total cost of purchasing allN NNitems.
当高桥以最优方式选择哪些商品以促销价购买时,求购买全部N NN件商品的最小可能总费用。

【输入】

N NNK KK
A 1 A_1A1B 1 B_1B1
A 2 A_2A2B 2 B_2B2
⋮ \vdots
A N A_NANB N B_NBN

  • The first line contains an integerN NNrepresenting the number of items and an integerK KKrepresenting the maximum number of items that can be purchased at the sale price, separated by a space.
  • The followingN NNlines each contain, for thei ii-th line( 1 ≤ i ≤ N ) (1 \leq i \leq N)(1iN), the regular priceA i A_iAiand the sale priceB i B_iBiof itemi ii, separated by a space.

【输出】

Print the minimum possible total cost of purchasing allN NNitems as an integer on a single line. Note that the answer may not fit within a32 3232-bit integer.

【输入样例】

3 2 100 80 200 150 300 250

【输出样例】

500

【解题思路】

【算法标签】

#贪心#

【代码详解】

#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=200005;intn,k,ans;// n: 物品数量,k: 打折商品数量,ans: 总花费structNode{intA,B;// A: 正常价格,B: 打折后价格}a[N];// 比较函数:按照 (A-B) 的差值从大到小排序// 即优先选择打折节省更多的商品boolcmp(Node x,Node y){returnx.A-x.B>y.A-y.B;}signedmain(){cin>>n>>k;// 读入商品总数和打折商品数量for(inti=1;i<=n;i++){cin>>a[i].A>>a[i].B;// 读入正常价格和打折后价格}// 按照差价从大到小排序sort(a+1,a+n+1,cmp);// 前k个商品选择打折价for(inti=1;i<=k;i++){ans+=a[i].B;}// 剩余商品选择原价for(inti=k+1;i<=n;i++){ans+=a[i].A;}cout<<ans<<endl;// 输出最小总花费return0;}

【运行结果】

3 2 100 80 200 150 300 250 500
http://www.jsqmd.com/news/694789/

相关文章:

  • AI SoC全芯片DFT实战
  • 别再只用enable password了!思科设备密码安全进阶:配置加密的enable secret与Console口超时
  • 深度强化学习与自然语言理解的融合实践
  • 手写一个分布式RPC框架!
  • AirSim安装报错‘No module named numpy’?一个隐藏的依赖陷阱与解决方案
  • 面试官最爱问的C++服务器项目:TinyWebServer中Epoll与Reactor模式如何协同工作?
  • 如何在 Realme 上恢复已删除的联系人
  • 【电能质量扰动】基于ML和DWT的电能质量扰动分类方法研究(Matlab实现)
  • 从零到一:手写笔迹还原算法(InkCanvas)的深度剖析与实战应用
  • Pycharm里用Conda环境跑Selenium总报错?这份避坑指南帮你一次搞定所有依赖和路径问题
  • ArcGIS新手必看:别再搞混OBJECTID、FID和OID了,一次讲清区别和实战用法
  • NLP实战入门——从零构建智能对话系统(一)
  • 芯片设计中的“普通话”和“方言”:LEF/DEF文件在物理实现中的角色与避坑指南
  • 告别盲调!用瑞萨RA_FSP的ADC监测MCU内部温度与电压,手把手搭建系统健康检查
  • 华为防火墙模拟器(eNSP)从零搭建实验环境:手把手配置管理口并开启Web登录
  • 题解:AtCoder AT_awc0003_d Consecutive Practice Days
  • NCMDump终极解密指南:3分钟解锁网易云音乐NCM加密格式
  • ArcGIS Pro连接Excel受阻?一文详解Microsoft驱动安装与静默部署
  • 从手机APP反推ESP32-C3蓝牙开发:看懂这些GATT数据,你就能改任何例程
  • Silvaco Athena实战:从零搭建一个0.8微米NMOS管,手把手教你调阈值电压和提取关键参数
  • 别再只复制Key了!高德地图Geocoder.getLocation本地调用完整避坑指南
  • YOLOv5训练避坑指南:batch-size设为8的倍数真的更快?聊聊数据对齐与显存‘浪费’的那些事
  • 【电液伺服执行器与PI控制器】带有PI控制器的电液伺服执行器的模拟研究(Simulink仿真实现)
  • 别再手动改PR了!教你写个ABAP报表,一键批量处理采购申请审批与信息更新
  • 分布式变分量子求解器在电力调度中的应用与优化
  • 从一次下载失败,聊聊TLS协议演进和那些被淘汰的‘老朋友’(附实战排查命令)
  • 如何从 iPhone 转移到 Realme:4 种简单方法
  • 保姆级拆解:用一张图看懂Wire Bonding的球焊与楔焊全流程(附常见缺陷图)
  • PyTorch音频处理实战:用torchaudio构建可微分的梅尔谱特征提取管道(适配GPU训练)
  • 反射半导体光放大器(RSOA)模型研究(Matlab代码实现)