• 贪心--cf、education62-C


    cf-Education 62-C

    题目

    C. Playlist

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You have a playlist consisting of nn songs. The ii-th song is characterized by two numbers titi and bibi — its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 33 songs having lengths [5,7,4][5,7,4] and beauty values [11,14,6][11,14,6] is equal to (5+7+4)⋅6=96(5+7+4)⋅6=96.

    You need to choose at most kk songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.

    Input

    The first line contains two integers nn and kk (1≤k≤n≤3⋅1051≤k≤n≤3⋅105) – the number of songs in the playlist and the maximum number of songs you can choose, respectively.

    Each of the next nn lines contains two integers titi and bibi (1≤ti,bi≤1061≤ti,bi≤106) — the length and beauty of ii-th song.

    Output

    Print one integer — the maximum pleasure you can get.

    Examples

    input

    Copy

    4 3
    4 7
    15 1
    3 6
    6 8
    

    output

    Copy

    78
    

    input

    Copy

    5 3
    12 31
    112 4
    100 100
    13 55
    55 50
    

    output

    Copy

    10000
    

    Note

    In the first test case we can choose songs 1,3,41,3,4, so the total pleasure is (4+3+6)⋅6=78(4+3+6)⋅6=78.

    In the second test case we can choose song 33. The total pleasure will be equal to 100⋅100=10000100⋅100=10000.

    题目大意

    给你n首歌,在里面最多选k首。每首歌有两个元素长度a和美丽值b,使选出来的歌中所有的长度的和sum乘以其中最低的b的积最大

    思路

    这类题一般是贪心。。。怎么贪就。。。看题目吧

    先把所有的歌按美丽值从小到大排序

    然后从后面开始遍历取歌,因为这样取,容易确定b的最小值(当前歌曲的b值),而且sum容易计算(计算队列中的sum值即可),就容易计算ans了。
    当队列中的歌曲数大于k时,弹出a值最小的,此时sum最大

    代码

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <cmath>
    #include <sstream>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <vector>
    #include <queue>
    #include <iomanip>
    #include <stack>
    
    using namespace std;
    
    typedef long long LL;
    const int INF = 0x3f3f3f3f;
    const int N = 300005;
    const int MOD = 1e9 + 9;
    const double pi = 3.1415926;
    
    #define lson l, m, rt << 1
    #define rson m + 1, r, rt << 1 | 1
    
    struct P
    {
        LL a, b;
    }p[N];
    
    bool cmp1(P x, P y)
    {
        return x.b < y.b;
    }
    
    struct cmp2
    {
        bool operator ()(const int &x, const int &y)
        {
            return x > y;
        }
    };
    int main()
    {
        int n, k;
        cin >> n >> k;
        priority_queue<int, vector<int>, cmp2> v;
        for(int i = 0;i < n;++i)
            cin >> p[i].a >> p[i].b;
        sort(p, p + n, cmp1);
        LL sum = 0, ans = 0;
        for(int i = n - 1;i >= 0;--i)
        {
            v.push(p[i].a);
            sum += p[i].a;
            if(v.size() > k)
            {
                sum -= v.top();
                v.pop();
            }
            ans = max(ans, sum * p[i].b);
        }
        cout << ans << endl;
        return 0;
    }
    
    
  • 相关阅读:
    poj 1200 crasy search
    cdoj 1092 韩爷的梦
    fzu 2257 saya的小熊饼干
    zoj 3950 how many nines
    zoj 3963 heap partion
    fzu 2256 迷宫
    fzu 2253 salty fish
    hdu 2473 Junk-Mail Filter
    codeforces 129B students and shoes
    hdu 3367 Pseudoforest
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/10586632.html
Copyright © 2020-2023  润新知