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 ...
随机推荐
- [题解向] 正睿Round435
10.14 Link 唔,这一场打得不好.获得了\(\rm 75pts/300pts\)的得分,但是居然可以获得\(\rm 27/69\)的名次,也不至于不满意--毕竟是真不会233 \(\rm T1 ...
- [LeetCode] 828. Unique Letter String 独特字符串
A character is unique in string S if it occurs exactly once in it. For example, in string S = " ...
- SpringBoot 2.x 整合Lombok
Lombok的官方介绍 Project Lombok is a java library that automatically plugs into your editor and build too ...
- LengthFieldBasedFrameDecoder 参数说明
LengthFieldBasedFrameDecoder 参数说明 举例 数据包格式为 body长度(4个)+14个字节的版本说明(字符串)+body 那么LengthFieldBasedFrameD ...
- SpringBoot之Swagger2文档生成
SpringBoot之Swagger2文档生成 1.Swagger2介绍 编写和维护接口文档是每个程序员的职责,前面我们已经写好的接口现在需要提供一份文档,这样才能方便调用者使用.考虑到编写接口文档是 ...
- windows 的一些快捷键
https://www.zhihu.com/question/276786944/answer/698967240 1.新建文件夹 Ctrl + Shift + N 或者鼠标右键 然后 ...
- 如何解决aws解绑银行卡问题?
首先先来说明一下我自己的情况? 一年的免费使用 前提:没有开启任何的实例服务 先贴一条官方的解释 关于我小白一个.学校课程要求使用aws,注册之后在网络上看到一堆人踩坑,aws的扣费就是个坑! 预授权 ...
- 【CTS2019】珍珠(生成函数)
[CTS2019]珍珠(生成函数) 题面 LOJ 洛谷 题解 lun题可海星. 首先一个大暴力\(sb\)的\(dp\)是设\(f[i][S]\)表示当前考虑完了前\(i\)个珍珠,\(S\)集合中这 ...
- Java 8——Base64工具
在java 8之前如果需要使用base64编解码,必须使用三方库,如:apache的commons-codec. 但是java 8将base64编解码的工具引入进来: public class Tes ...
- eclipse中修改项目名
把项目名springboot-demo改成springboot-rabbitmq 第一步: 选中项目,点击F2,修改项目名第二步: 修改.project文件第三步: 修改.setting/org.ec ...