链接:https://ac.nowcoder.com/acm/contest/5678/G
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
西安邮电大学有一辆从老校区到新校区的校车,总共有 n 个学生乘坐校车,在 ai站上车,在 bi站下车。学校打算去除一部分不必要的站点,请问需要保留多少站点,需要安排多少个座位?
输入描述:
输出描述:
输出保留站点数,座位数。
示例1
输入
1
3
1 2
1 3
2 4
输出
4 2
解题思路:站点数统计一下不同上下车站即可,座位数则为交集处最多的区间数,可以将所有车站线看成一个轴,每次有人上车人数加1,下车人数减1,动态处理上下车过程人数最大值即可。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5+5;
int main() {
int t,n,a,b;
cin >> t;
while(t--) {
int cnt = 0;
map<int,int> mp1;
map<int,int> mp;
cin >> n;
for(int i=0;i<n;i++) {
cin >> a >> b;
if(!mp1[a])cnt++,mp1[a] = 1;
if(!mp1[b])cnt++,mp1[b] = 1;
mp[a]++,mp[b]--;
}
int sum = 0,maxx = -1;
for(map<int,int>::iterator it = mp.begin();it!=mp.end();it++) {
sum += it->second;
maxx = max(maxx,sum);
}
cout << cnt << ' ' << maxx << endl;
}
return 0;
}