1 //只需要按照结束时间排序贪心即可 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 typedef struct 6 { 7 int x,y; 8 }P; 9 P p[10005]; 10 bool cmp(P a,P b) 11 { 12 if(a.y < b.y) return true; 13 else if(a.y == b.y && a.x > b.x) return true; 14 return false; 15 } 16 int main() 17 { 18 int t,n; 19 cin >> t; 20 while(t--) 21 { 22 cin >> n; 23 for(int i=0; i<n; ++i) 24 cin >> p[i].x >> p[i].y; 25 sort(p,p+n,cmp); 26 int ans = 1,temp = p[0].y; 27 for(int i=1; i<n; ++i) 28 if(p[i].x > temp) 29 { 30 ++ans; 31 temp = p[i].y; 32 } 33 cout << ans << endl; 34 } 35 return 0; 36 }
1 //最优程序 2 #include<stdio.h> 3 #include <vector> 4 #include<algorithm> 5 #include<math.h> 6 using namespace std; 7 8 struct activ 9 { 10 int begin; 11 int end; 12 }; 13 bool cmp( activ a,activ b) 14 { 15 return a.end<b.end; 16 } 17 int main() 18 { 19 //freopen("1.txt","r",stdin); 20 int n; 21 scanf("%d",&n); 22 while (n--) 23 { 24 int m; 25 scanf("%d",&m); 26 vector<activ> vec; 27 for(int i=0;i<m;i++) 28 { 29 activ a; 30 scanf("%d%d",&a.begin,&a.end); 31 vec.push_back(a); 32 } 33 34 sort(vec.begin(),vec.end(),cmp); 35 int count=vec.size(); 36 int k=0; 37 for (int i=1;i<vec.size();i++) 38 { 39 if(vec[i].begin <= vec[k].end) 40 count--; 41 else k=i; 42 } 43 printf("%d\n",count); 44 } 45 46 return 0; 47 }
时间: 2024-11-07 10:54:14