B - Mix Juice


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 200200 points

Problem Statement

A shop sells NN kinds of fruits, Fruit 1,…,N1,…,N, at prices of p1,…,pNp1,…,pN yen per item, respectively. (Yen is the currency of Japan.)

Here, we will choose KK kinds of fruits and buy one of each chosen kind. Find the minimum possible total price of those fruits.

Constraints

  • 1≤K≤N≤10001≤K≤N≤1000
  • 1≤pi≤10001≤pi≤1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN KK
p1p1 p2p2 …… pNpN

Output

Print an integer representing the minimum possible total price of fruits.


Sample Input 1 Copy

Copy
5 3
50 100 80 120 80

Sample Output 1 Copy

Copy
210

This shop sells Fruit 11, 22, 33, 44, and 55 for 5050 yen, 100100 yen, 8080 yen, 120120 yen, and 8080 yen, respectively.

The minimum total price for three kinds of fruits is 50+80+80=21050+80+80=210 yen when choosing Fruit 11, 33, and 55.


Sample Input 2 Copy

Copy
1 1
1000

Sample Output 2 Copy

Copy
1000
题意:输入一个n,然后输入n个数,找出前k小的数的和,并输出
思路:此题解法多种,可以是用优先队列来做也可以对数组排序来做。
优先队列:
#include<cstdio>
#include<queue>
using namespace std;
int main(void)
{int n,k;while(~scanf("%d%d",&n,&k)){priority_queue<int,vector<int>,greater<int> >p;//小顶堆for(int i=0;i<n;++i){int t;scanf("%d",&t);p.push(t);}int sum=0;for(int i=0;i<k;++i)sum+=p.top(),p.pop();printf("%d\n",sum);while(!p.empty())p.pop();}return 0;
}


排序:

#include<cstdio>
#include<algorithm>
#define maxn 1005
using namespace std;
bool cmp(int x,int y)
{return x<y;
}
int main(void)
{int n,k;int a[maxn];while(~scanf("%d%d",&n,&k)){for(int i=0;i<n;++i)scanf("%d",&a[i]);sort(a,a+n,cmp);int sum=0;for(int i=0;i<k;++i)sum+=a[i];printf("%d\n",sum);}return 0;
}

关于sort的用法可以参考这个博客:https://www.cnblogs.com/YHH520/p/12253671.html

AtCoder Beginner Contest 171 B - Mix Juice相关推荐

  1. AtCoder Beginner Contest 171 D Replacing 桶排序+及时更新数据

    AtCoder Beginner Contest 171   比赛人数10532  比赛开始后14分钟看到A题,之后,每过一分钟看到一道题 AtCoder Beginner Contest 171  ...

  2. AtCoder Beginner Contest 171 C.One Quadrillion and One Dalmatians

    AtCoder Beginner Contest 171 C.One Quadrillion and One Dalmatians 题目链接 这题真的坑啊,我想到了 26 进制却一直卡细节,因为 x ...

  3. AtCoder Beginner Contest 171 E - Red Scarf

    E - Red Scarf Time Limit: 2 sec / Memory Limit: 1024 MB Score : 500500 points Problem Statement Ther ...

  4. AtCoder Beginner Contest 171 A - αlphabet

    A - αlphabet Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100 points Problem Statement An upper ...

  5. AtCoder Beginner Contest 171 D - Replacing

    D - Replacing Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement You ...

  6. AtCoder Beginner Contest 202 D - aab aba baa(组合计数,字典序)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Problem 有 AAA 和 aaa,BBB 个 bbb ,可以使用这 A+BA+BA+B 个字符任 ...

  7. AtCoder Beginner Contest 197 题解(A ~ F)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Rotate B - Visibility C - ORXOR D - Opposite ...

  8. AtCoder Beginner Contest 198 (A ~ F)题解

    目录 A. Div B. Palindrome with leading zeros C. Compass Walking D. Send More Money E. Unique Color F. ...

  9. AtCoder Beginner Contest 215 G - Colorful Candies 2

    AtCoder Beginner Contest 215 G - Colorful Candies 2 有n个糖果,每个糖果有着一个颜色a[i],每次拿k个糖果期望拿到E(x)个不同颜色的糖果,求出k ...

最新文章

  1. rk3399在linux机上烧写img,烧写固件 — TB-96AI documentation
  2. aida64副屏监控_“遥信”在电力监控系统中的重要作用
  3. POPUP_TO_CONFIRM_STEP
  4. TCP三次握手,握的是啥?
  5. CF1516E. Baby Ehab Plays with Permutations(组合数学)
  6. node.js express 启用 https
  7. 数据结构 - 二叉排序树BST(创建、遍历、删除节点)
  8. (王道408考研数据结构)第五章树-第四节2:平衡二叉树(AVL)及其旋转
  9. Java是世界上最好的语言!
  10. 将字符串中的html标签编译,将字符串中的HTML标签包含的内容移除
  11. 卷积神经网络训练准确率突然下降_基于联邦学习和卷积神经网络的入侵检测方法...
  12. RabbitMQ学习——生产者与消费者入门例子
  13. 【erlang ~ 4 days】 Day # 1.1 History
  14. 物理学 物体的运动力学分析之牛顿三定律 单摆的MATLAB运动仿真(一)
  15. ESB(企业服务总线)
  16. nginx 搭建静态网站
  17. 图像去噪,深度学习去噪,普通方法
  18. 安装 xorg-macros
  19. 引用腾讯视频 php代码,调用腾讯视频播放器APi代码
  20. springboot集成easyExcel实现文件导入导出

热门文章

  1. SQL数据查询之——单表查询
  2. linux下mysql 启动命令
  3. 借教室(codevs 1217)
  4. SpringMVC 上传图片保存到服务器 同时更改图片名称保存至数据库
  5. 【剑指offer】面试题五:从尾到头打印链表
  6. Linux Shell编程(3)——运行shell脚本
  7. ZYNQ 调试遇到的种种问题汇总[转帖]
  8. 学习Javascript闭包(Closure)(转载+理解心得)
  9. Python random模块sample、randint、shuffle、choice随机函数
  10. SQL Server中授予用户查看对象定义的权限