Codeforces A. Playlist(暴力剪枝)

题目描述:

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

posted @ 2019-08-16 15:25  小张人  阅读(260)  评论(0编辑  收藏  举报
分享到: