P2434 [SDOI2005]区间

题目:传送门

思路:贪心

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 struct node{
 6     int a;
 7     int b;
 8 };
 9 node p[50000];
10 int n=0;
11 struct cmp{
12     bool operator()(node m,node n){
13         return m.a<n.a;
14     }
15 };
16 int main(){
17     cin>>n;
18     for(int i=0;i<n;i++){
19         cin>>p[i].a>>p[i].b;
20     }
21     sort(p,p+n,cmp());            //根据a的大小对结构体排序 
22     for(int i=0;i<n;i++){
23         if(p[i].b<p[i+1].a){
24             cout<<p[i].a<<" "<<p[i].b<<endl;
25         }else{
26             cout<<p[i].a<<" ";
27             int t=i;
28             while((p[t].b>=p[i+1].a)&&i<n){    //当p[i].b<p[i+1].a则跳出来 
29                 if(p[t].b<p[i+1].b)    t=i+1;
30                 i++; 
31             }
32             cout<<p[t].b<<endl;
33         }
34     }
35     return 0;
36 }

 

posted @ 2020-05-02 17:18  neverstopcoding  阅读(139)  评论(0编辑  收藏  举报