贪心,Gene Assembly,ZOJ(1076)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=76

解题报告:

1、类似活动安排问题。

2、输出格式要注意。

#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;


struct gene
{
    int s;///起始
    int f;///结束
    int index;///编号
} a[1005];

bool b[1005];

bool cmp(const gene&a,const gene&b)
{
    if(a.f<=b.f)
        return true;
    else return false;
}

int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        memset(b,false,sizeof(b));
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&a[i].s,&a[i].f);
            a[i].index=i+1;
        }
        sort(a,a+n,cmp);
        b[0]=true;
        int PreEnd=0;
        for(int i=1; i<n; i++)
        {
            if(a[i].s>a[PreEnd].f)
            {
                b[i]=true;
                PreEnd=i;
            }
        }
        printf("%d",a[0].index);
        for(int i=1; i<n; i++)
            if(b[i]) printf(" %d",a[i].index);
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/TreeDream/p/5283072.html