【题目大意】

给出n个区间,问最多选取多少个区间使得它们互相不重叠。

【思路】

水题quq改善心情用。按照右端点大小排序,每次更新上一次的右端点,如果当前左端点大于上次右端点可取。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int MAXN=50000+50;
 7 struct node
 8 {
 9     int l,r; 
10     bool operator < (const node &x) const
11     {
12         return (r<x.r);
13     }
14 }cow[MAXN];
15 int n;
16 
17 int main()
18 {
19     scanf("%d",&n);
20     for (int i=0;i<n;i++) scanf("%d%d",&cow[i].l,&cow[i].r);
21     sort(cow,cow+n);
22     int r=-1,ans=0;
23     for (int i=0;i<n;i++)
24     {
25         if (cow[i].l>=r)
26         {
27             r=cow[i].r;
28             ans++;
29         }
30     }
31     cout<<ans<<endl;
32     return 0;
33 }

 

相关文章:

  • 2021-08-17
  • 2021-07-20
  • 2021-08-11
  • 2021-05-17
  • 2021-10-16
  • 2022-02-13
  • 2022-01-11
猜你喜欢
  • 2022-01-12
  • 2022-12-23
  • 2021-10-01
  • 2021-11-22
  • 2021-11-30
  • 2021-11-29
  • 2021-05-25
相关资源
相似解决方案