最大不相交区间数量
演唱会、计算最多能观看几场演出
样例1
输入
2
720 120
840 120
输出
1
样例2
输入
2
0 60
75 60
输出
2
C++代码
// Problem: #OD268. 演唱会、计算最多能观看几场演出
// Contest: Hydro
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 区间问题:求最大不相交区间数量 https://www.acwing.com/problem/content/910/
#include <algorithm>
#include <iostream>using namespace std;const int N = 1010;int n;struct Node
{int l, r;bool operator<(const Node &t) const{return r < t.r;}
} node[N];void solve()
{sort(node, node + n);int res = 0, ed = -2e9;for (int i = 0; i < n; i++){if (ed <= node[i].l){//注意本题区间端点可以重合,所以有等号,(0,60)与(60,75)算两个区间res++;ed = node[i].r;}}cout << res;
}int main()
{ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);cin >> n;for (int i = 0; i < n; i++){int l, len;cin >> l >> len;int r = l + len + 15;node[i] = {l, r};}solve();return 0;
}