想来想去还是应该记录一下这个题
和流星那个题一样,也是求拥有最大个数的时间(从区间段里找)
可以也用struct,就像流星那个题一样(刘老师的方法)
但是还有一种用vector<pair<int, int>也很好用很方便
因为vector本身就是按照first值从小到大排序,如果first一样了,就按second从小到大排序
所以我们可以把右端点事件的second值设为-1,左端点事件second值设为1
//1.struct#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;const int N = 100010;struct Event {int time;int type;bool operator < (const Event& a) const {return (time < a.time || (time == a.time && type > a.type));}
}event[2 *N];int main() {int n, b, d;scanf("%d", &n);int cnt = 0;for (int i = 1; i <= n; i++) {scanf("%d %d", &b, &d);event[cnt++] = Event{ b, 0 };event[cnt++] = Event{ d, 1 };}sort(event, event + cnt);int ans = 0, p = 0, sum = 0;for (int i = 0; i < cnt; i++) {if (event[i].type == 0) {sum++;if (sum > ans) ans = sum, p = i;}else sum--;}cout << event[p].time << " " << ans << endl;return 0;
}
//2.vector#include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;const int N = 100010;
typedef pair<int, int> PII;vector<PII> v;int main() {int n, b, d;cin >> n;int y, k = 0, num = 0;for (int i = 0; i < n; i++) {cin >> b >> d;v.push_back({ b, 1 });v.push_back({ d, -1 });}sort(v.begin(), v.end());for (auto i : v) {if (i.second == 1) {num++;if (num > k) k = num, y = i.first;}else num--;}cout << y << " " << k;return 0;
}
ps:注意vector的用法,不要写成vector<int,int> ! 要把<int, int>变成pair<int, int>再放到vector中;
所以是vector<PII>