区间合并
原题链接不准确!!!
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n;
typedef pair<int, int> PII;
vector<PII> query;
int main()
{
cin >> n;
vector<PII> res;
while (n--)
{
int l, r;
cin >> l >> r;
query.push_back({l, r});
}
// 先进行排序
sort(query.begin(), query.end());
int st = -2e9, ed = -2e9;
for (auto item : query)
{
if (ed < item.first)
{ // 不能够合并 找到了新的区间
if (st != -2e9) // 不能够是初始的区间
res.push_back({st, ed});
st = item.first;
ed = item.second;
}
else
{ // 有交集 能够合并
ed = max(ed, item.second);
}
}
// 只计算最后一个区间
if (st != -2e9)
res.push_back({st, ed});
cout << res.size();
return 0;
}