[luoguP2434] [SDOI2005]区间(贪心)

传送门

简单贪心

——代码

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 
 5 int n, l, r;
 6 struct node
 7 {
 8     int x, y;
 9 }p[50001];
10 
11 inline bool cmp(node x, node y)
12 {
13     return x.x < y.x;
14 }
15 
16 inline int max(int x, int y)
17 {
18     return x > y ? x : y;
19 }
20 
21 int main()
22 {
23     int i;
24     scanf("%d", &n);
25     for(i = 1; i <= n; i++) scanf("%d %d", &p[i].x, &p[i].y);
26     std::sort(p + 1, p + n + 1, cmp);
27     l = p[1].x;
28     r = p[1].y;
29     for(i = 2; i <= n; i++)
30     {
31         if(p[i].x > r)
32         {
33             printf("%d %d
", l, r);
34             r = 0;
35         }
36         if(!r)
37         {
38             l = p[i].x;
39             r = p[i].y;
40         }
41         else r = max(r, p[i].y);
42     }
43     printf("%d %d
", l, r);
44     return 0;
45 }
View Code
原文地址:https://www.cnblogs.com/zhenghaotian/p/6908176.html