hdu 2037(最简单的贪心算法)

来源:互联网 发布:mac php 版本 编辑:程序博客网 时间:2024/05/24 00:18

http://acm.hdu.edu.cn/showproblem.php?pid=2037
把队列按照,结束时间从小大到排列。然后依次遍历,如果开始时间大于前面最后的结束时间就是可行方案,并记录结束时间。
代码:

#include<iostream>using namespace std;struct tv{    int s;    int e;}a[105];int cmp(const void*a,const void*b){    if (((tv*)a)->e == ((tv*)b)->e)        return ((tv*)a)->s - ((tv*)b)->s;    return ((tv*)a)->e - ((tv*)b)->e;}int n;int main(){    while (cin>>n&&n)    {        for (int i = 0; i < n; i++){            cin >> a[i].s >> a[i].e;        }        qsort(a, n, sizeof(a[1]), cmp);        int num = 0, l = 0;        for (int i = 0; i < n; i++){            if (l <= a[i].s){                num++;                l = a[i].e;            }        }        cout << num << endl;    }}
0 0
原创粉丝点击