C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列
2 seconds
256 megabytes
standard input
standard output
You have a playlist consisting of nn songs. The ii-th song is characterized by two numbers titi and bibi — 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 33 songs having lengths [5,7,4][5,7,4] and beauty values [11,14,6][11,14,6] is equal to (5+7+4)⋅6=96(5+7+4)⋅6=96.
You need to choose at most kk songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
The first line contains two integers nn and kk (1≤k≤n≤3⋅1051≤k≤n≤3⋅105) – the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next nn lines contains two integers titi and bibi (1≤ti,bi≤1061≤ti,bi≤106) — the length and beauty of ii-th song.
Print one integer — the maximum pleasure you can get.
4 3
4 7
15 1
3 6
6 8
78
5 3
12 31
112 4
100 100
13 55
55 50
10000
In the first test case we can choose songs 1,3,41,3,4, so the total pleasure is (4+3+6)⋅6=78(4+3+6)⋅6=78.
In the second test case we can choose song 33. The total pleasure will be equal to 100⋅100=10000100⋅100=10000.
这个题目要好好想想,一开始以为是dp,因为数据太大可以发现肯定不是dp,后来想到用贪心,但是并不会写。。。
实际上这个是贪心+优先队列,用优先队列来处理,然后进行贪心。
优先队列怎么处理呢,首先我们对b进行排序,b大的放在前面,然后把每一个数的t值都放进队列里面,t越小放在越前面,
然后如果这个队列的size大于k,就要清除头部的那个元素,但是这个时候之后的取最大值并不会受到影响,因为如果是刚刚放进去
的被pop出来了,那sum肯定没有变大,而且同时b变小了,所以这个时候就不会对这里进行转移,如果是别的元素pop出来了,
那就没有影响了。
这里要想清楚为什么是对b进行排序,想清楚为什么对b进行排序之后进行优先队列的处理,就是最优的。
这个我之前没有想清楚,所以就写题目的时候就有各种天马行空的想法。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 100;
struct node
{
int t, b;
node(int t=0,int b=0):t(t),b(b){}
bool operator<(const node&a)const
{
if (a.b == this->b) return a.t < this->t;
return a.b < this->b;
}
}exa[maxn];
priority_queue<ll, vector<ll>, greater<ll>>que;
int main()
{
ll sum = 0,ans=0;
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%d%d", &exa[i].t, &exa[i].b);
sort(exa + 1, exa + 1 + n);
for(int i=1;i<=n;i++)
{
sum += exa[i].t;
que.push(exa[i].t);
if(que.size()>k)
{
sum -= que.top();
que.pop();
}
//printf("exa[%d]=%d %lld %lld\n", i, exa[i].t, sum,ans);
ans = max(ans, sum*exa[i].b);
}
printf("%lld\n", ans);
return 0;
}
C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列的更多相关文章
- Educational Codeforces Round 62 (Rated for Div. 2) - C Playlist
当时题意看错了...不过大致思路是对的,唯一没有想到的就是用优先队列搞这个东西,真是不该啊... 题意大概就是,有N首歌,N首歌有两个东西,一个是长度Ti,一个是美丽值Bi,你最多可以选择K首歌, 这 ...
- Educational Codeforces Round 62 (Rated for Div. 2)C
题目链接 :C. Playlist #include<bits/stdc++.h> using namespace std; #define maxn 300005 #define LL ...
- Educational Codeforces Round 62 (Rated for Div. 2) Solution
最近省队前联考被杭二成七南外什么的吊锤得布星,拿一场Div. 2恢复信心 然后Div.2 Rk3.Div. 1+Div. 2 Rk9,rating大涨200引起舒适 现在的Div. 2都怎么了,最难题 ...
- Educational Codeforces Round 62 (Rated for Div. 2)
A. Detective Book 题意:一个人读书 给出每一章埋的坑在第几页可以填完 . 一个人一天如果不填完坑他就会一直看 问几天能把这本书看完 思路:模拟一下 取一下过程中最大的坑的页数 如 ...
- Educational Codeforces Round 62 (Rated for Div. 2) C 贪心 + 优先队列 + 反向处理
https://codeforces.com/contest/1140/problem/C 题意 每首歌有\(t_i\)和\(b_i\)两个值,最多挑选m首歌,使得sum(\(t_i\))*min(\ ...
- Educational Codeforces Round 62 (Rated for Div. 2)E(染色DP,构造,思维,组合数学)
#include<bits/stdc++.h>using namespace std;const long long mod=998244353;long long f[200007][2 ...
- C. Brutality Educational Codeforces Round 59 (Rated for Div. 2) 贪心+思维
C. Brutality time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
随机推荐
- c#等程序中的关于时间的最大值【DateTime.MaxValue】和最小值【DateTime.MinValue】
运行之后得到的结果 c# DateTime.MaxValue:// :: DateTime.MinValue:// :: Sql Server DateTime 类型必须介于 1/1/1753 12: ...
- Aquarium Tank(csu1634+几何+二分)Contest2087 - 湖南多校对抗赛(2015.05.24)-G
Aquarium Tank Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 15 Solved: 4[Submit][Status][Web Board ...
- RPC调用与GC垃圾回收
RPC调用 多个服务协同完成一次业务时,由于业务约束(如红包不符合使用条件.账户余额不足等).系统故障(如网络或系统超时或中断.数据库约束不满足等),都可能造成服务处理过程在任何一步无法继续,使数据处 ...
- linux mail操作
本操作系统邮件由来,crontab定时任务执行推送产生. 1.查看有多少封邮件 & file 2.我们直接键入23935来访问这封mail,看看是否是我们所需要的最新邮件. 3. 退出邮件查看 ...
- PHP微信H5支付
今天项目用到了微信新出的h5支付直接去官网 https://pay.weixin.qq.com/wiki/doc/api/index.html找dome去了找了之后才发现没有一脸懵逼,一开始以为和公众 ...
- JavaScript for/in 语句 遍历数组内容
for-in遍历 for-in是为遍历对象而设计的,不适用于遍历数组. 遍历数组的缺点:数组的下标index值是数字,for-in遍历的index值"0","1" ...
- 前端常用技术概述--Less、typescript与webpack
前言:讲起前端,我们就不能不讲CSS与Javascript,在这两种技术广泛应用的今天,他们的扩展也是层出不穷,css的扩展有Less.Sass.Stylus 等,js的超集有Typescript等. ...
- iOS ----------关于动画
这个网址上的内容很不错.https://github.com/ameizi/DevArticles/issues/91
- Simplest Python K-Way Merging Sort|最简单的Python k路归并排序
想做这个好长时间了,因为有一篇Dreamworks的论文<Coherent Out-of-Core Point-Based Global Illumination>提到了这个,一直没时间做 ...
- 「Android」GreenDao
译文 版本:greenDAO 3.2.2 官网:http://greenrobot.org/greendao/ GitHub:https://github.com/greenrobot/greenDA ...