1 条题解

  • 0
    @ 2025-8-22 20:36:54

    贪心例题

    不难发现,在此题中局部最优解可以导致全局最优解,符合贪心前提

    开贪!!!

    #include<bits/stdc++.h>
    using namespace std;
    struct node{
    	int s,f;
    }q[1010];
    int n,k,num;
    bool cmp(node x,node y){
    	if(x.f!=y.f)return x.f<y.f;
    	return x.s<y.s;
    }
    int main(){cin>>n;
    	for(int i=0;i<n;i++)cin>>q[i].s>>q[i].f;
    	sort(q,q+n,cmp);k=q[0].f,num=1;
    	for(int i=1;i<n;i++)
    		if(k<=q[i].s)num++,k=q[i].f;
    	printf("%d",num);
    	return 0;
    }
    
    • 1

    信息

    ID
    22
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    22
    已通过
    2
    上传者