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 贪心 + 优先队列 + 反向处理的更多相关文章

  1. Educational Codeforces Round 62 (Rated for Div. 2) Solution

    最近省队前联考被杭二成七南外什么的吊锤得布星,拿一场Div. 2恢复信心 然后Div.2 Rk3.Div. 1+Div. 2 Rk9,rating大涨200引起舒适 现在的Div. 2都怎么了,最难题 ...

  2. Educational Codeforces Round 62 (Rated for Div. 2)

    A. Detective Book 题意:一个人读书  给出每一章埋的坑在第几页可以填完 . 一个人一天如果不填完坑他就会一直看 问几天能把这本书看完 思路:模拟一下 取一下过程中最大的坑的页数  如 ...

  3. Educational Codeforces Round 62 (Rated for Div. 2)C

    题目链接 :C. Playlist #include<bits/stdc++.h> using namespace std; #define maxn 300005 #define LL ...

  4. Educational Codeforces Round 62 (Rated for Div. 2) - C Playlist

    当时题意看错了...不过大致思路是对的,唯一没有想到的就是用优先队列搞这个东西,真是不该啊... 题意大概就是,有N首歌,N首歌有两个东西,一个是长度Ti,一个是美丽值Bi,你最多可以选择K首歌, 这 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

随机推荐

  1. 20165312 2017-2018-2《Java程序设计》第9周学习总结

    20165312 2017-2018-2<Java程序设计>第9周学习总结 上周错题总结 1.进程的基本状态有:新建.运行.阻塞.死亡. A . true B . false 解析:A 这 ...

  2. Make a plan, and stand for it!

    我发现博主本人善于事前做计划,事后做总结.但是不善于坚持自己的计划.就拿10.1这个假期来讲,放假前看多许多的攻略,计划了很多条的自驾出行的路线,但是好像一条也没坚持,最后选择了一条临时的线路,而且临 ...

  3. Linux背背背(4)vim操作

    目录 1.打开文件 2.vim的三种模式 3.扩展 (关于vi 和 vim 的区别,它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面.) 1 ...

  4. μC/Probe尝鲜

    μC/Probe 1.添加文件 2.配置probe_com_cfg.h 2.1.选择接口 #define PROBE_COM_CFG_RS232_EN DEF_ENABLED /* Configure ...

  5. python中定时任务

    今天看网络框架时,突然想看一下定时器,于是往上搜索了一下python中timer task的实现,但是由于python本身对线程的支持不是太好,因为全局排它锁的存在,使得多线程在访问资源时效率比较低. ...

  6. leetcode14:最长公共字符串

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  7. 2101244 - FAQ: SAP HANA Multitenant Database Containers (MDC)

    Symptom You face issues or have questions related to multitenant database containers in SAP HANA env ...

  8. Webform(条件查询)

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  9. leetcode46

    public class Solution { public IList<IList<int>> Permute(int[] nums) { IList<IList< ...

  10. 3Linux常用命令

    文件目录管理命令 1.touch touch 文件名   #创建空白文件 -a  修改读取(访问)时间atime -m  修改修改时间mtime -d  同时修改atime 和 mtime touch ...