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. Activiti图表bpmn对应的xml文件

    当我们绘制了一个bpmn图表,实质是创建了一个xml文件, 右键bpmn文件open  with   xml edit <?xml version="1.0" encodin ...

  2. java 栈 最大深度

      1. 概述 某公司面试,总监大叔过来,问了图论及栈的最大深度,然后^_^ 一直记着,今天搞一下 2. 代码 package com.goodfan.test; public class JavaS ...

  3. MATLAB——矩阵排序详解

    <span style="font-size:18px;">(1)B=sort(A) 对一维或二维数组进行升序排序,并返回排序后的数组,当A为二维时,对数组每一列进行排 ...

  4. leetcode739

    class Solution(object): def dailyTemperatures(self, T: 'List[int]') -> 'List[int]': S = list() n ...

  5. docker 部署tomcat

    使用Docker搭建Tomcat运行环境 1 准备宿主系统 准备一个 CentOS 7操作系统,具体要求如下: 必须是 64 位操作系统 建议内核在 3.8 以上 通过以下命令查看您的 CentOS ...

  6. 转: rem与px的转换

    rem是相对于根元素<html>,这样就意味着,我们只需要在根元素确定一个参考值,这个参考值设置为多少,完全可以根据您自己的需求来定.· 我们知道,浏览器默认的字号16px,来看一些px单 ...

  7. python,opencv,imread,imwrite,存储,读取图像像素不一致,这种情况是label使用jpg格式

    最近在做图像分割,需要使用一些分割图片的label,但是发现存储的分割label感觉被平滑过了,即使使用 image = cv2.imread(info['path'],cv2.IMREAD_UNCH ...

  8. Python设计模式 - 基础 - 类/接口之间的六种关系

    在程序中需要把世间万物抽象成相应的类,现实世界中物与物之间的关系和程序中类与类之间的关系相对应,因为世间万物是普遍联系的,所以程序中类与类之间也不是孤立的.在系统分析和框架设计中,根据面向对象机制的三 ...

  9. c++ 面试题(C/C++/STL)

    1,智能指针:auto_ptr(c++11 已经弃用),unique_ptr(用于取代 auto_ptr),  shared_ptr,  weak_ptr http://www.cnblogs.com ...

  10. 20175314 《Java程序设计》迭代和JDB

    20175314 <Java程序设计>迭代和JDB 要求 1 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能 2 m,n 要通过命令 ...