凌乱的yyy / 线段覆盖(贪心)

https://www.luogu.org/problemnew/show/P1803  题目链接

贪心,选择结束时间为关键字排序,相同时开始时间大的在前,然后for一遍比较就好了 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<set>
 7 #include<vector>
 8 #include<stack>
 9 #include<queue>
10 #include<map>
11 using namespace std;
12 #define ll long long
13 #define se second
14 #define fi first
15 const int INF= 0x3f3f3f3f;
16 const int N=1e6+5;
17 
18 int n;
19 
20 struct note
21 {
22     int a;
23     int b;
24 }p[N];
25 
26 bool cmp(note x,note y)
27 {
28     return (x.b<y.b || x.b==y.b&&x.a>y.a);//按b小的排,相等时按a大的排
29 }
30 int main()
31 {
32     scanf("%d",&n);
33     for(int i=1;i<=n;i++) scanf("%d %d",&p[i].a,&p[i].b);
34     sort(p+1,p+1+n,cmp);
35     int cnt=1,u=p[1].b;
36     for(int i=2;i<=n;i++)
37     {
38         if(p[i].a>= u)
39             cnt++, u=p[i].b;
40     }
41     cout<<cnt;
42 }

猜你喜欢

转载自www.cnblogs.com/thunder-110/p/9283264.html
yyy