hdu 2037(今年暑假不AC)

题目:

       有n个区间,[ai, bi), 统计不相交区间最多有多少个?

贪心策略:

       将这n个区间按bi由小到大排序,然后从前向后遍历,每当遇到不相交的区间就加入目标集合,遍历完成后就找到了最多的不相交区间。

具体证明,上篇博客有:

#include<stdio.h>
#include<algorithm>
using namespace std;
struct Node
{
    int s,e;
}N[105];
bool cmp(Node a,Node b)
{
    if(a.e<b.e)return true;
    //else return a.s<b.s;
    return false;
}
int main()
{
    int n;
    int count,end;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        for(int i=0;i<n;i++)
        scanf("%d %d",&N[i].s,&N[i].e);
        sort(N,N+n,cmp);
        end=-1;
        count=0;
        for(int i=0;i<n;i++)
        {
            if(end<=N[i].s)
            {
                end=N[i].e;
                count++;
            }
        }
        printf("%d\n",count);
    }

    return 0;
}

@@:

bool cmp(Node a,Node b)
{
    if(a.e<b.e)return true;
    //else return a.s<b.s;
    return false;
}
在bool变量类型中:不能用 return a-b;的语句
原文地址:https://www.cnblogs.com/XDJjy/p/3018673.html