题目描述:

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 -th song is characterized by two numbers t**i — 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 and beauty values [11,14,6].

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 (1≤kn≤3⋅105)

Each of the next n lines contains two integers and b**i) — the length and beauty of i

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,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\).

思路:

题目是说给一系列歌曲,歌曲有两个属性,长度和幸福度。选择不超过k首歌,歌的总幸福度等于(长度和)乘上(最小幸福度)。要求所有选择方案中最大的总幸福度。

刚开始时属于完全摸不着头脑,然后想着贪心怎么选择,发现很难找出一个策略来,因为局部的最优信息好像和全局最优信息搭不上边。然后想暴力。怎么暴力?由于题目里面要求很特殊的一点是幸福度是选择的歌曲里面最小的。很自然而然想到排序。建一个结构体,包含两个上述属性,根据bi(幸福度)排序。

一开始想到的是从小到大排序。然后遍历数组,遍历到的元素只能选后面幸福度比他大的(前面的只能选后面的),这样就保证了幸福度就是当前这首歌的幸福度。又因为总幸福度等于长度和*最小幸福度,所以还要选出长度和最大的k-1首歌,就对这个元素后面的所有元素按长度从大到小排序,选前k-1个(如果不够k-1个就都选)。这样能保证当前最小幸福度下的总幸福度最大。及时更新总幸福度的最大值,最后得到答案。这样子算时间复杂度是\(O(n^2log_2n)\),看下数据范围\(3 *10^5\),嗯,妥妥的超时。

然后说正解。正解也是排序,不过是按幸福度从大到小排而不是从小到大。遍历数组,同时维护一个按长度从小到大的优先级队列。不断选元素到队列中,如果队列元素个数不足k,就继续加,如果超过k,就把长度最小的元素移出队列,加入当前元素在队列中。在这个过程中不断记录并更新最大幸福度。可实现\(O(nlog_2k)\)的时间复杂度。注意的是这里不是贪心,而是暴力枚举,不过是有技巧的暴力。

这里说明一下为什么这样子枚举会把最大值包含进去。当队列没有满的时候,我们在遍历数组的过程中不断加数进去,在这个过程中更新最大值,显然最大值可能在这种情况下出现。因为

如果\(1.\)说现在队列里已有元素但没有满,那么遍历到当前元素时,如果最大值出以当前元素为最小幸福度,那么它只可能包括队列里的前面的所有元素(长度最长)。

如果2.现在队列没有元素,这种情况只发生在开始的时候,如果最大值以当前元素为最小幸福度,那么最大值就是当前元素(没有比他幸福度大的元素)乘它的长度。

如果\(3.\)如果队列满了,遍历到当前元素,如果最大值以当前元素为最小幸福度,那么弹出长度最小的(弹出的幸福度一定大于等于当前元素的幸福度),往队列加入当前元素后就是可能的最大值。那为什么这个时候的队列里的元素组合就是以当前元素为最小幸福度的最大值的可能值呢?因为经过上面的筛选,剩在队列里的是比当前元素幸福度大或等且最长的元素。(这就相当于我上面解法中的选幸福度比他大的最长k个元素,但由于我是排序选的,复杂度上去了)

由此可见,上面算法的过程就是一个剪枝的过程,把最大值可能值的不可能出现的组合直接略过,只枚举了最大值可能出现的值。十分巧妙。

代码:

#include <iostream>
#include <queue>
#include <algorithm>
#define max_n 300005
using namespace std;
int n;
int k;
struct node
{
long long ti;
long long bi;
};
int cmp(node a,node b)
{
return a.bi>b.bi;
}
node a[max_n];
int main()
{
cin >> n >> k;
for(int i = 0;i<n;i++)
{
cin >> a[i].ti >> a[i].bi;
}
sort(a,a+n,cmp);
priority_queue<int,vector<int>,greater<int> > que;
long long maxm = 0;
long long sum = 0;
for(int i = 0;i<n;i++)
{
if(que.size()<k)
{
que.push(a[i].ti);
sum += a[i].ti;
maxm = max(maxm,sum*a[i].bi);
}
else
{
long long len = que.top();
que.pop();
sum -= len;
len = a[i].ti;
sum += len;
que.push(len);
maxm = max(maxm,sum*a[i].bi);
}
}
cout << maxm << endl;
return 0;
}

参考文章:

LightningUZ,Codeforces 1140C 题解,https://blog.csdn.net/LightningUZ/article/details/89036190

Codeforces A. Playlist(暴力剪枝)的更多相关文章

  1. poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】

    题目:  http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...

  2. codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)

    You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the ...

  3. Codeforces 1185G2 Playlist for Polycarp (hard version) 背包,暴力

    题意及思路:https://www.cnblogs.com/Als123/p/11061147.html 代码: #include <bits/stdc++.h> #define LL l ...

  4. Chladni Figure CodeForces - 1162D (暴力,真香啊~)

    Chladni Figure CodeForces - 1162D Inaka has a disc, the circumference of which is nn units. The circ ...

  5. HDU 5839 Special Tetrahedron (2016CCPC网络赛08) (暴力+剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5839 在一个三维坐标,给你n个点,问你有多少个四面体(4个点,6条边) 且满足至少四边相等 其余两边不 ...

  6. HDU 4876 ZCC loves cards(暴力剪枝)

    HDU 4876 ZCC loves cards 题目链接 题意:给定一些卡片,每一个卡片上有数字,如今选k个卡片,绕成一个环,每次能够再这个环上连续选1 - k张卡片,得到他们的异或和的数,给定一个 ...

  7. acdream 小晴天老师系列——晴天的后花园 (暴力+剪枝)

    小晴天老师系列——晴天的后花园 Time Limit: 10000/5000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others) ...

  8. HDU 6382 odds (暴力 + 剪枝优化)

    odds Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Subm ...

  9. Karen and Game CodeForces - 816C (暴力+构造)

    On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as fo ...

随机推荐

  1. js对url进行编码的方法(encodeURI和 encodeURICompoent())

    encodeURI(): 对整个URL进行编码,对应的解码方式:decodeURI() encodeURIComponent() : 对查询字符串进行编码,对应的解码方式:decodeURICompo ...

  2. 【2019.7.25 NOIP模拟赛 T1】变换(change)(思维+大分类讨论)

    几个性质 我们通过推式子可以发现: \[B⇒AC⇒AAB⇒AAAC⇒C\] \[C⇒AB⇒AAC⇒AAAB⇒B\] 也就是说: 性质一: \(B,C\)可以相互转换. 则我们再次推式子可以发现: \[ ...

  3. 8.19 NOIP模拟测试26(B) 嚎叫响彻在贪婪的厂房+主仆见证了 Hobo 的离别+征途堆积出友情的永恒

    T1 嚎叫响彻在贪婪的厂房 以前做过一个等比数列的题「序列」,这个类似 是等差数列且公差不为1的条件就是各项差的绝对值的$gcd!=1$,每次拿出序列前两个数,求出差值,插入到set里,每次向后扩展, ...

  4. [LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  5. GreenPlum 大数据平台--segment 失效问题恢复

    1,问题检查 [gpadmin@greenplum01 conf]$ psql -c "select * from gp_segment_configuration where status ...

  6. markdown格式接口文档模板

    源文件 https://files.cnblogs.com/files/bincoding/%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3.zip 目录 测试接口 查询指定项 ...

  7. 二进制安装K8S集群V1.16.3

    centos linux7.5 cat > /etc/hosts << EOF 192.168.199.221 master 192.168.199.222 node1 192.16 ...

  8. springboot kafka 消费者

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  9. GC分析工具使用-gceacy分析堆栈

    gceasy是一款在线的gc分析工具.试用一下分析jstack的日志 1.jstack -l 3539 > 3539.stack 2.打包成zip文件 3.上传https://gceasy.io ...

  10. no main manifest attribute, in testProject-1.0-SNAPSHOT.jar

    no main manifest attribute, in testProject-1.0-SNAPSHOT.jar 错误描述: no main manifest attribute, in tes ...