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< ...
随机推荐
- aspose.cells 插入图片
,,"d:\\1.jpg"); Aspose.Cells.Drawing.Picture pic = worksheet.Pictures[iIndex]; pic.Placeme ...
- word embeddding和keras中的embedding
训练好的词向量模型被保存下来,该模型的本质就是一个m*n的矩阵,m代表训练语料中词的个数,n代表训练时我们设定的词向量维度.当我们训练好模型后再次调用时,就可以从该模型中直接获取到对应词的词向量. 通 ...
- 教你phpstudy如何搭建本地多站点
经常做多个网站同时开发,如何才能在本地能使部署多个站点,今天就来分享一下如何用PHPstudy搭建本地多站点. 点击上图中的 其它选项菜单 ,就会弹出下面的对话框,然后点击 站点域名管理 然后在 网站 ...
- SpringMVC - 运行流程图及原理分析
流程示意图: 代码分析图:
- c# 对DataTable进行分组group by
]);//对索引为0的一列进行分组,结果是集合
- javac编译错误: 编码UTF8/GBK的不可映射字符
转自:https://blog.csdn.net/leytton/article/details/52740171 Linux下为UTF-8编码,javac编译gbk编码的java文件时,容易出现“错 ...
- Xeon Phi 《协处理器高性能编程指南》随书代码整理 part 2
▶ 第四章,逐步优化了一个三维卷积计算的过程 ● 基准代码 #include <stdio.h> #include <stdlib.h> #include <string ...
- Linux命令:readonly
readonly [-aAf] [name[=value] ...] or readonly -p -A 表示后面的name变量都是关联数组 -a 表示后面的name变量都是index数组 -f 表示 ...
- 译:SOS_SCHEDULER_YIELD类型等待在虚拟机环境中的增多
原文出处:Increased SOS_SCHEDULER_YIELD waits on virtual machines 注: 原文的用词是Increased,想译作增强(增长),或者加强,这么译起来 ...
- 最新ceph集群常用命令梳理
结合网络.官网.手动查询等多方渠道,整理ceph维护管理常用命令,并且梳理常规命令在使用过程中的逻辑顺序.另外整理期间发现ceph 集群的命令体系有点乱,详细情况各自体验. 一:ceph集群启动.重启 ...