cf Educational Codeforces Round 62 C. Playlist

原题:
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 n songs. The i-th song is characterized by two numbers ti and bi — 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 3 songs having lengths [5,7,4] and beauty values [11,14,6] is equal to (5+7+4)⋅6=96.

You need to choose at most k 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 n and k (1≤k≤n≤3⋅10^5 ) – the number of songs in the playlist and the maximum number of songs you can choose, respectively.

Each of the next n lines contains two integers ti and bi (1≤ti,bi≤10^6) — the length and beauty of i-th song.

Output
Print one integer — the maximum pleasure you can get.

Examples
Input

4 3
4 7
15 1
3 6
6 8

Output

78

Input

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

Output

10000

Note

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

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

中文:
有n首歌,每首歌有两个值,分别是歌曲长度和愉悦感。现在让你选k首歌,得到的最终的总长度是这k首歌的长度和,愉悦感是这k首歌中最小值。

现在让你选k首歌,使得选出的k首歌的愉悦感与总长度的乘积最大。

代码:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<stack>
    #include<queue>
    using namespace std;
     
     
    const int maxn = 300005;
     
     
    typedef long long ll;
    typedef pair<ll, ll> pii;
     
    int n, k;
    pii ps[maxn];
    /*
    4 2
    14 15
    90 10
    80 8
    100 7
     
    */
     
    struct cmp
    {
    	bool operator()(pii& p1, pii& p2)
    	{
    		return p1.first>p2.first;
    	}
    };
     
    int main()
    {
    	ios::sync_with_stdio(false);
    	while (cin >> n >> k)
    	{
    		for (int i = 1; i <= n; i++)
    			cin >> ps[i].first >> ps[i].second;
    		sort(ps + 1, ps + 1 + n, [](pii &p1, pii &p2)
    		{
    			if (p1.second != p2.second)
    				return p1.second>p2.second;
    			else
    				return p1.first>p2.first;
    		});
     
    		ll len = 0, ans = 0;
     
    		priority_queue<pii, vector<pii>, cmp> Q;
     
    		for (int i = 1; i <= n; i++)
    		{
    			Q.push(ps[i]);
    			len += ps[i].first;
    			if (Q.size()>k)
    			{
    				len -= Q.top().first;
    				Q.pop();
    			}
    			ans = max(ans, len*ps[i].second);
     
    		}
    		cout << ans << endl;
    	}
    	return 0;
    }

思路:

将n首歌按照愉悦感从大到小排序,维持一个容量为k的堆,堆顶元素为当前歌曲长度最小的歌,遍历歌曲时,每次更新当前歌曲的愉悦感值,由于歌曲按照愉悦感从小到大排序,愉悦感肯定越来越小,每次更新记录当前k首的总长度乘以当前最小愉悦感即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值