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(\(b_i\))最大
题解
- 假如最小的\(b_i\)确定了,那么拿得越多越好
- 枚举最小的\(b_i\)然后取剩下最大的\(t_i\)
- 两种做法
1.从\(b_i\)大向小扫,这样用优先队列维护最大的那些\(t_i\)(丢弃最小的)
2.用两个优先队列模拟
代码
//做法1
#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
#define ft first
#define se second
using namespace std;
priority_queue<ll,vector<ll>,greater<ll> >Q;
pii p[300005];
ll ans,sum;
int n,k;
int main(){
cin>>n>>k;
for(int i=0;i<n;i++)scanf("%lld%lld",&p[i].se,&p[i].ft);
sort(p,p+n,greater<pii>());
for(int i=0;i<k;i++){
Q.push(p[i].se);
sum+=p[i].se;
ans=max(ans,sum*p[i].ft);
}
for(int i=k;i<n;i++){
ll mi=Q.top();
if(p[i].se<=mi)continue;
else{
Q.pop();
Q.push(p[i].se);
sum+=p[i].se-mi;
ans=max(ans,sum*p[i].ft);
}
}
cout<<ans;
}
//做法2
#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
#define mk make_pair
#define ft first
#define se second
using namespace std;
struct N{
ll t,d;
}p[300005];
ll n,k,ans,i,tim;
struct cmp{
bool operator() (pii a, pii b ){
if(a.ft==b.ft)return a.se>b.se;
return a.ft>b.ft;
}
};
bool cmp1(N x,N y){
return x.d<y.d;
}
priority_queue<pii,vector<pii>,cmp> s;
priority_queue<pii>q;
int main(){
cin>>n>>k;
for(i=1;i<=n;i++){
scanf("%lld%lld",&p[i].t,&p[i].d);
q.push(mk(p[i].t,p[i].d));
}
sort(p+1,p+n+1,cmp1);
tim=0;
for(i=1;i<=n;i++){
while(!s.empty()&&s.top().ft<p[i].d){
//cout<<"s "<<s.top().se<<endl;
tim-=s.top().se;s.pop();
}
ans=max(ans,p[i].d*tim);
while(!q.empty()&&s.size()<k){
//cout<<"q "<<q.top().ft<<endl;
if(q.top().se>=p[i].d){
tim+=q.top().ft;
s.push(mk(q.top().se,q.top().ft));
}
q.pop();
ans=max(ans,p[i].d*tim);
}
}
cout<<ans;
}
Educational Codeforces Round 62 (Rated for Div. 2) C 贪心 + 优先队列 + 反向处理的更多相关文章
- 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
题目链接 :C. Playlist #include<bits/stdc++.h> using namespace std; #define maxn 300005 #define LL ...
- Educational Codeforces Round 62 (Rated for Div. 2) - C Playlist
当时题意看错了...不过大致思路是对的,唯一没有想到的就是用优先队列搞这个东西,真是不该啊... 题意大概就是,有N首歌,N首歌有两个东西,一个是长度Ti,一个是美丽值Bi,你最多可以选择K首歌, 这 ...
- C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列
C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
随机推荐
- base64 压缩上传上传图片
@{ ViewBag.Title = "dddddddd"; Layout = "~/Areas/Wap/Views/Shared/_Head.cshtml"; ...
- tp5 (layui )excel导入
1.composer安装PHPExcel 下载安装composer 其次 cmd切换到项目根目录 运行命令:composer require phpoffice/phpexcel 注意: 1.运行可能 ...
- WebService . Schema约束
1. namespace 相当于schema文件的id 2. targetNamespace属性 用来指定schema文件的namespace的值 3. xmlns属性 引入一个约束, 它的值是一个s ...
- Android倒计时实现
Android为我们封装好了一个抽象类CountDownTimer,可以实现计时器功能: /** * 倒数计时器 */ private CountDownTimer timer = new Count ...
- ActiveMQ(1)---初识ActiveMQ
消息中间件的初步认识 什么是消息中间件? 消息中间件是值利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,可以在分布式架构下扩展进 ...
- 3. tomcat 内存设置
-Xms512m -Xmx1024m -XX:PermSize=512M
- Rhythmk 一步一步学 JAVA(6): JSP 语法学习笔记
1.修改JSP页面模版: 找到MyEclips安装目录,搜索“Jsp.vtl”,找到该文件修改编码,以及一些不需要用到的代码. 2.查找项目生成的Servlet文件路径: 查看当前项目父级目录搜索 . ...
- JAVA字符串类
一.字符串类String1.String是一个类,位于java.lang包中2.创建一个字符串对象的2种方式: String 变量名=“值”; String 对象名=new String(“值”);3 ...
- python入门(八):文件操作
1.数据的保存: 1) 内存:常用的变量都是在内存里面的 缺点:关机或进程死掉数据丢失 解决方法:将数据保存至文件中 2 )文件:文本内容.二进制的文件内容 3 )数据库:保存 2.读文件: 1 ...
- 洛谷P1169 棋盘制作(悬线法)
题目链接:https://www.luogu.org/problemnew/show/P1169 #include<bits/stdc++.h> #define fi first #def ...