HDOJ 2037
1 #include<iostream> 2 using namespace std; 3 4 #include<vector> 5 #include<math.h> 6 #include<iomanip> 7 #include<string.h> 8 9 void swap(int &a,int &b) 10 { 11 int c = a; 12 a = b; 13 b = c; 14 } 15 16 int partition(vector<int> &vs,vector<int> &ve,int start,int end) 17 { 18 int a = vs[start]; 19 int b = ve[start]; 20 while(start < end) 21 { 22 while(start < end && vs[end] >= a) --end; 23 if(start < end) 24 { 25 vs[start] = vs[end]; 26 ve[start] = ve[end]; 27 } 28 while(start < end && vs[start] <= a) ++start; 29 if(start < end) 30 { 31 vs[end] = vs[start]; 32 ve[end] = ve[start]; 33 } 34 } 35 vs[start] = a; 36 ve[start] = b; 37 return start; 38 } 39 40 void quickSort(vector<int> &vs,vector<int> &ve,int start,int end) 41 { 42 if(start < end) 43 { 44 int pos = partition(vs,ve,start,end); 45 quickSort(vs,ve,start,pos-1); 46 quickSort(vs,ve,pos+1,end); 47 } 48 } 49 50 int max(int count[],int start,int end) 51 { 52 int c = count[start]; 53 for(int i = start+1;i < end;i++) 54 { 55 if(c < count[i]) c = count[i]; 56 } 57 return c; 58 } 59 60 int findFirst(vector<int> vs,int a,int end) 61 { 62 int i = 0; 63 while(vs[i] < a && i <= end) i++; 64 if(i > end) return -1; 65 return i; 66 } 67 68 int main() 69 { 70 int n,s,e,c,maxcount; 71 while(cin>>n && n != 0) 72 { 73 c = 0; 74 int count[n],i; 75 memset(count,0,sizeof(int)*n); 76 vector<int> vs,ve; 77 for(i = 0;i < n;i++) 78 { 79 cin>>s>>e; 80 vs.push_back(s); 81 ve.push_back(e); 82 } 83 quickSort(vs,ve,0,vs.size()-1); 84 for(i = 1;i < vs.size();) 85 { 86 while(vs[i] == vs[i-1]) 87 { 88 ++c; 89 ++i; 90 } 91 quickSort(ve,vs,i-c-1,i-1); 92 c = 0; 93 ++i; 94 } 95 i = vs.size()-1; 96 while(vs[i] == vs[i-1]) 97 { 98 count[i] = 1; 99 --i; 100 } 101 count[i] = 1; 102 --i; 103 maxcount = 1; 104 for(;i >= 0;--i) 105 { 106 int start = findFirst(vs,ve[i],vs.size()-1); 107 if(start == -1) 108 { 109 count[i] = 1; 110 continue; 111 } 112 count[i] = 1+max(count,start,vs.size()-1); 113 if(maxcount < count[i]) maxcount = count[i]; 114 } 115 116 cout<<maxcount<<endl; 117 } 118 119 return 0; 120 }