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 ...
随机推荐
- 判断所有的input框不能为空
// 判断input框(除了类名为min1和max7)是否为空 function isEmpty(){ var flag=true; $("input[type='text']") ...
- 洛谷p1137旅行计划
题面 关于拓扑排序 因为这好几次考试的题目里都有在DAG中拓扑排序求最长/短路 txt说它非常的好用 就找了个题做了下 拓扑排序就是寻找图中所有的入度为零的点把他入队 然后再枚举它所有的连到的点,只要 ...
- [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- [LeetCode] 454. 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- shell脚本特殊变量($0、$1、$2、 $?、 $# 、$@、 $*)
$0 Shell本身的文件名$1-$n 添加到Shell的各参数值.$1是第1参数.$2是第2参数…$$ Shell本身的PID(ProcessID) $! ...
- java的GUI之SWT框架 配置开发环境(包含但不限于WindowBuilder完整教程,解决Unknown GUI toolkit报错,解决导入SWT包错误)
官网(资料挺多的,API文档截图以及示例都有):https://www.eclipse.org/swt/ 克隆官方仓库 git clone --depth=1 git://git.eclipse.or ...
- JavaScript 系列--JavaScript一些奇淫技巧的实现方法(二)数字格式化 1234567890转1,234,567,890;argruments 对象(类数组)转换成数组
一.前言 之前写了一篇文章:JavaScript 系列--JavaScript一些奇淫技巧的实现方法(一)简短的sleep函数,获取时间戳 https://www.mwcxs.top/page/746 ...
- 第23课 优先选用make系列函数
一. make系列函数 (一)三个make函数 1. std::make_shared:用于创建shared_ptr.GCC编译器中,其内部是通过调用std::allocate_shared来实现的. ...
- C# 递增操作符 ++ --
记混了好几次,记录一下 递增操作符出现在操作数之前:先递增后赋值 ; int result; result =++count; Console.WriteLine($"count:{coun ...
- .net 中访问config的一些方式
人所缺乏的不是才干而是志向,不是成功的能力而是勤劳的意志. 哎!好久没有写博客了,今天就分享一些比较常用的对config文件的访问一些方式. 首先 引用 using System.Configurat ...