问题描述
强大的kAc建立了强大的帝国,但人民深受其学霸及23文化的压迫,于是勇敢的鹏决心反抗。
kAc帝国防守森严,鹏带领着小伙伴们躲在城外的草堆叶子中,称为叶子鹏。
kAc帝国的派出的n个看守员都发现了这一问题,第i个人会告诉你在第li个草堆到第ri个草堆里面有人,要求你计算所有草堆中最少的人数,以商议应对。
“你为什么这么厉害”,得到过kAc衷心赞美的你必将全力以赴。
输入格式
第一行一个数字n,接下来2到n+1行,每行两个数li和ri,如题。
输出格式
输出一个数,表示最少人数。
样例输入
5
2 4
1 3
5 7
1 8
8 8
样例输出
3
数据规模和约定
30%的数据n<=10
70%的数据n<=100
100%的数据n<=1000
所有数字均在int表示范围内
思路:实际上是去找整个区间的所有交集个数,即对所有区间求交集,剩下的交集个数即为所求。
代码如下:
用了链表以及其结构体排序,方便删除,同时练习一下链表的知识点
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
typedef long long ll;
using namespace std;
ll n = 0;
struct section {
ll l = -1;//左边界
ll r = -1;//右边界
};
bool cmp(section &a, section &b) {
return a.r < b.r;
}
vector<ll> ve;
int main() {
cin >> n;
list<section>lis;
section se;
for (ll i = 0; i < n; i++) {
ll temp1 = 0, temp2 = 0;
cin >> temp1 >> temp2;
se.l = temp1; se.r = temp2;
lis.push_back(se);
}
lis.sort(cmp);//链表中的结构体升序排序
list<section>::iterator it = lis.begin();
while (!lis.empty()) {
it = lis.begin();
ll mr = it->r;//mr为r最小的数
ve.push_back(mr);
for (; it != lis.end();) {
if (it->l <= mr && it->r >= mr) {//删除包含mr的区间
lis.erase(it++);
}
else it++;
}
}
cout << ve.size();
return 0;
}