Files
python/TangDou/AcWing/Section/906_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

36 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int b[2 * N]; // key,value:第几个端点,坐标值
int idx; // 用于维护数组b的游标
int n; // 共几个区间
int res = 1; // 全放到一个组中,最小,默认值1
int main() {
// n个区间
cin >> n;
for (int i = 1; i <= n; i++) {
int l, r;
cin >> l >> r;
b[idx++] = l * 2 + 1; // 标记左端点为奇数;同比放大2倍,还不影响排序的位置,牛~
b[idx++] = r * 2; // 标记右端点为偶数;同比放大2倍,还不影响排序的位置,牛~
}
// 将所有端点放在一起排序,由小到大
sort(b, b + idx);
int t = 0;
for (int i = 0; i < idx; i++) {
if (b[i] % 2)
t++; // 左端点
else
t--; // 右端点
res = max(res, t); // 动态计算什么时间点时,出现左的个数减去右的个数差最大,就是冲突最多的时刻
}
// 输出结果
cout << res << endl;
return 0;
}