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≤k≤n≤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(暴力剪枝)的更多相关文章
- poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】
题目: http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...
- codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)
You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the ...
- Codeforces 1185G2 Playlist for Polycarp (hard version) 背包,暴力
题意及思路:https://www.cnblogs.com/Als123/p/11061147.html 代码: #include <bits/stdc++.h> #define LL l ...
- Chladni Figure CodeForces - 1162D (暴力,真香啊~)
Chladni Figure CodeForces - 1162D Inaka has a disc, the circumference of which is nn units. The circ ...
- HDU 5839 Special Tetrahedron (2016CCPC网络赛08) (暴力+剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5839 在一个三维坐标,给你n个点,问你有多少个四面体(4个点,6条边) 且满足至少四边相等 其余两边不 ...
- HDU 4876 ZCC loves cards(暴力剪枝)
HDU 4876 ZCC loves cards 题目链接 题意:给定一些卡片,每一个卡片上有数字,如今选k个卡片,绕成一个环,每次能够再这个环上连续选1 - k张卡片,得到他们的异或和的数,给定一个 ...
- acdream 小晴天老师系列——晴天的后花园 (暴力+剪枝)
小晴天老师系列——晴天的后花园 Time Limit: 10000/5000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) ...
- HDU 6382 odds (暴力 + 剪枝优化)
odds Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Subm ...
- 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 ...
随机推荐
- 【Arch安装】
[Arch安装]不完整,凭记忆补充 1,制作安装介质(请跳转链接:https://www.archlinux.org/download/) 2,从UEFI模式启动后,按照官方WIKI向导操作(http ...
- intellij idea 解决2019年4月到期延期问题
56ZS5PQ1RF-eyJsaWNlbnNlSWQiOiI1NlpTNVBRMVJGIiwibGljZW5zZWVOYW1lIjoi5q2j54mI5o6I5p2DIC4iLCJhc3NpZ25lZ ...
- Linux record
1.设置ubuntu密码刚安装好的ubuntu系统,没有root密码,需要用户去手动设置的. sudo passwd root 输入2次密码即可. 2. Linux下is not in the sud ...
- sort和sorted
sort 与 sorted 区别: sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作. list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 ...
- 19条常用的MySQL优化方法(转)
本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下:1.EXPLAIN命令做MySQL优化,我们要善用EXPLAIN查看SQL执行计划.下面来个简单的示例,标注(1.2.3.4.5)我们 ...
- BScroll使用
当页面内容的高度超过视口高度的时候,会出现纵向滚动条:当页面内容的宽度超过视口宽度的时候,会出现横向滚动条.也就是当我们的视口展示不下内容的时候,会通过滚动条的方式让用户滚动屏幕看到剩余的内容. 话说 ...
- protobuf 中import 的使用
目录结构如下: test.proto的文件内容如下: syntax="proto2"; package com.eagle.mohrss; option java_outer_cl ...
- 《 .NET并发编程实战》阅读指南 - 第13章
先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.
- Python - Win10系统下Python3.x环境配置
Win10系统下Python3.x环境配置 https://blog.csdn.net/qq_41952474/article/details/82630551
- Laravel处理session(会话)的方法详解
在Web应用程序中,有必要识别跨越请求的用户并为每个用户保存数据,为此,像Laravel这样的框架提供了一种称为会话的机制.本篇文章就来为大家介绍关于Laravel处理session(会话)的方法. ...