zoukankan      html  css  js  c++  java
  • AtCoder Beginner Contest 171 B

    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

    • 1KN10001≤K≤N≤1000
    • 1pi10001≤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 11223344, 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 1133, 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
    ",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
    ",sum);
        }
        return 0;
    }

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

    
    
  • 相关阅读:
    计算机的基本存储单位
    挖坑
    HEOI2017 游记
    bzoj4815 [Cqoi2017]小Q的表格
    bzoj4817 [Sdoi2017]树点涂色
    hdu5824 graph
    4.5&4.7联考题解
    高飞
    无题
    51Nod 算法马拉松23 开黑记
  • 原文地址:https://www.cnblogs.com/Mangata/p/13307915.html
Copyright © 2011-2022 走看看