题解:AtCoder AT_awc0001_c Discount Coupon
本文分享的必刷题目是从蓝桥云课、洛谷、AcWing等知名刷题平台精心挑选而来并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。欢迎大家订阅我的专栏算法题解C与Python实现附上汇总贴算法竞赛备考冲刺必刷题C | 汇总【题目来源】AtCoderC - Discount Coupon【题目描述】Takahashi is about to go shopping on an online shopping site.高桥即将在一个网购网站上进行购物。The shopping site hasN NNitems, and each itemi iihas a price ofD i D_iDiyen. Takahashi plans to purchase all the items.该购物网站有N NN件商品其中每件商品i ii的价格为D i D_iDi日元。高桥计划购买所有商品。Takahashi has a special discount coupon. By using this coupon, he can reduce the price of selected items to0 00yen. However, there is a restriction that the coupon can be applied to at mostK KKitems.高桥拥有一张特殊折扣券。使用此券他可以将选定商品的价格降至0 00日元。但有一项限制此券最多可应用于K KK件商品。Takahashi wants to use the coupon optimally to minimize the total amount he pays. Find the minimum total amount he needs to pay when using the coupon optimally.高桥希望以最优方式使用折扣券以最小化他的总支付金额。求在最优使用折扣券的情况下他需要支付的最小总金额。【输入】N NNK KKD 1 D 2 … D N D_1\ D_2\ \dots\ D_ND1D2…DNThe first line containsN NN, representing the number of items, andK KK, representing the maximum number of items the coupon can be applied to, separated by a space.The second line containsD 1 , D 2 , … , D N D_1,D_2,…,D_ND1,D2,…,DN, representing the price of each item, separated by spaces.【输出】Output the minimum total amount to pay in one line.【输入样例】5 2 100 250 300 150 200【输出样例】450【解题思路】【算法标签】#贪心#【代码详解】#includebits/stdc.husingnamespacestd;#defineintlonglongconstintN200005;intn,k,ans;// n: 商品数量k: 折扣商品数量ans: 总花费intd[N];// 存储每个商品的价格signedmain(){cinnk;// 读入商品数量和折扣商品数量for(inti1;in;i){cind[i];// 读入每个商品的价格}sort(d1,dn1);// 将商品价格从小到大排序// 选择前n-k个最便宜的商品即放弃k个最贵的商品获得折扣for(inti1;in-k;i){ansd[i];// 累加这n-k个商品的价格}coutansendl;// 输出总花费return0;}【运行结果】5 2 100 250 300 150 200 450