Friday, 8 May 2015

FIND MAXIMUM NO OF OF NOT INTERSECTING LINES FROM A GIVEN SET OF LINES FROM A GIVEN SET OF LINES ............

EX--

Sample

inputoutput
5
3 4
1 5
6 7
4 5
1 3
3
--------------------------------------EDITORIAL-----------------------------------------------------------------
SORT ARRAY ACCORDING TO FIRST .. NOW LET US ASSUME THAT 1 ST ELEMENTE IN THE SORTED ARRY IS OUR 1 ST LINE .. NOW ITERATE OVER  REST ALL POINT .IF GET ANY POINT WITH  START LESS THAN END OF LAST COVERED LINE THEN TAKE THAT LINE AND INCREASE THE COUNT ELSE IF END  POINT OF NEXT LINE IS LESS THAN END POINT OF LAST PICKED LINE THEN PIC THIS  LINE AND LEFT LAST LINE   MEANS JUST CHANGE END OF LAST LINE PICKED TO END OF CURRENT LINE ....


-----------------------------------------------------CODE----------------------------------------------------------
#include<iostream>
#define  ff first
#define ss second
using namespace std;
#include<bits/stdc++.h>
int main()
 {
  int n;
   cin>>n;
   vector<pair<int,int> > v;
    for(int i=0;i<n;i++)
     {
      int a,b;
      cin>>a>>b;
       v.push_back(make_pair(a,b));
     
     }
     sort(v.begin(),v.end());
     int count =1;
     int last=v[0].second;
      for(int i=1;i<n;i++)
       {
        if(v[i].ff>last)
        {
        count++;
        last=v[i].ss;
        }
        else
         {
          if(v[i].ss<last) last=v[i].ss;
         }
       }
        cout<<count<<endl;
 }  

No comments:

Post a Comment