hdu 1051

http://acm.hdu.edu.cn/showproblem.php?pid=1051

这题思路跟1257那道差不多。。。半贪心半dp

代码如下:

#include"stdio.h"
#include"stdlib.h"

int a[5005][2],w[5005][2];
struct node 
{
    int x,y;
}bar[5005];

int cmp(const void *a,const void *b)
{
    node *aa=(node*)a,*bb=(node*)b;
    if(aa->x!=bb->x)
        return aa->x-bb->x;
    else
        return aa->y-bb->y;
}

int main( )
{
    int n,t,i,count,flag,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%d%d",&a[i][0],&a[i][1]);
        qsort(a,n,sizeof(node),cmp);
        count=1;
        w[0][0]=a[0][0];w[0][1]=a[0][1];
        for(i=1;i<n;i++)
        {
            flag=0;
            for(j=0;j<count;j++)
            {
                if(a[i][0]>=w[j][0]&&a[i][1]>=w[j][1])
                {
                    flag=1;
                    w[j][0]=a[i][0];w[j][1]=a[i][1];
                    break;
                }
            }
            if(flag==0)
            {
                w[count][0]=a[i][0];
                w[count][1]=a[i][1];
                count++;
            }
        }
        printf("%d\n",count);
    }
    return 0;
}
posted @ 2012-05-20 03:55  朝圣の路  阅读(409)  评论(0编辑  收藏  举报