问题描述
有 n个会议需要使用学校的会议室,第 i 个会议的长度为 ci ,最晚在 ti时刻结束。会议室某一时间点只能供一场会议使用,并且一场会议只可以选择取消或者完整的开完。求最多可以安排多少个会议。
输入格式
第一行包含一个整数 n。
接下来 n 行每行包含两个整数 ti,ci。
输出格式
输出一个整数,表示最多可以安排的会议数量。
样例输入
5
4 3
3 2
5 3
2 1
6 1
样例输出
3
数据规模和约定
对于 30%
的数据, n≤100;
对于 100%
的数据, n≤105,ti≤109,ci≤109 。
思路解析
大体思路为贪心
排序后能多开会就多开会
但遇到更短的会议就把已经开的会议中的最长会议替换
上述步骤用set或者priority_queue
#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
struct node{
int c,t;
}a[maxn];
int n;
priority_queue<int> q;
bool cmp(node x,node y){
if(x.t==y.t){
return x.c<y.c;
}
return x.t<y.t;
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i].t>>a[i].c;
}
sort(a,a+n,cmp);
int time=0,cnt=0;
for(int i=0;i<n;i++){
if(time+a[i].c<=a[i].t){ //能开会就开会
q.push(a[i].c);
time+=a[i].c;
cnt++;
}else if(!q.empty()&&a[i].c<q.top()){
if(time-q.top()+a[i].c<=a[i].t){ // 将最长的会议换成短的
time-=q.top();
time+=a[i].c;
q.pop();
q.push(a[i].c);
}
}
}
cout<<cnt<<endl;
return 0;
}