hdu2037 今年暑假不AC

来源:互联网 发布:服装零售收银软件 编辑:程序博客网 时间:2024/05/18 01:01

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2037
题意:中文题
解析:贪心水题,按右区间排序从小到大排序,每次拿出一个和前一个的右区间进行比较,合法ans++,并且更新记录

#include <bits/stdc++.h>using namespace std;struct node{    int l,r;    bool operator < (const node b)const    {        return r<b.r;    }}a[104];int main(void){    int n;    while(~scanf("%d",&n)&&n)    {        for(int i=0;i<n;i++)            scanf("%d %d",&a[i].l,&a[i].r);        sort(a,a+n);        int ans = 1;        int last = a[0].r;        for(int i=1;i<n;i++)        {            if(a[i].l>=last)            {                ans++;                last = a[i].r;            }        }        printf("%d\n",ans);    }    return 0;}
原创粉丝点击