2 solutions

  • 2
    @ 2024-7-5 21:09:59

    就是活动选择改个等号

    #include<bits/stdc++.h>
    using namespace std;
    struct s{
        int b,e;
    }arr[1010];
    bool cmp(s a,s b){
        return a.e<b.e;
    }
    bool flag[32769];
    int main(){
        int n,ans=1;
        cin>>n;
        for(int i=0;i<n;i++) cin>>arr[i].b>>arr[i].e;
        sort(arr,arr+n,cmp);
        int last=arr[0].e;
        for(int i=1;i<n;i++){
            if(arr[i].b>last){
                last=arr[i].e;
                ans++;
            }
        }
        cout<<ans;
        return 0;
    }
    
    • 0
      @ 2025-3-24 13:25:45
      #include<iostream>
      #include<algorithm>
      #include<cmath>
      #include<cstdio>
      using namespace std;
      
      struct node{
      	int x,y;
      	bool operator <(const node &o)const{
      		return x<o.x;
      	}
      };
      int n,cnt,w=-1e9+7;//初始最小值 
      node a[100010];
      int main(){
      	cin>>n;
      	for(int i=1;i<=n;i++){
      		//区间起始位置,区间终止位置 
      		cin>>a[i].x>>a[i].y;
      	}
      	//区间起始位置位置小的优先 
      	sort(a+1,a+1+n);
      	for(int i=1;i<=n;i++){
      		if(a[i].x>w){//起始位置不在现在范围内时 
      			w=a[i].y;//范围扩张 
      			cnt++;//个数+1 
      		}
      	}
      	cout<<cnt;
      	return 0;
      }
      
      • 1

      Information

      ID
      809
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      5
      Tags
      # Submissions
      50
      Accepted
      19
      Uploaded By