首页 > 其他 > 详细

nyoj 220 推桌子 poj 1083

时间:2014-04-16 15:41:42      阅读:512      评论:0      收藏:0      [点我收藏+]


题意:

给出一些区间,让你每次从中选取一些互不相交的区间,让选择次数最少。


解法:贪心 或 dp


贪心思路:

首先按初始点排序,然后依次向后能选择可以加入的,不能加入的第二次选择,输出次数即可、


代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

struct Node
{
    int a,b;
};
Node s[300];
int ans[300];
int comp(Node x,Node y)
{
    if(x.a!=y.a)
        return x.a<y.a;
}
int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int cmx,cmy;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&cmx,&cmy);
            cmx=(cmx+1)/2,cmy=(cmy+1)/2;
            if(cmx>cmy)
            {
                int temp=cmx;
                cmx=cmy;
                cmy=temp;
            }
            s[i].a=cmx,s[i].b=cmy;
        }
        sort(s,s+n,comp);
        int cas=0,last;
        memset(ans,0,sizeof(ans));
        for(int i=0; i<n; i++)
        {
            if(ans[i]==0)
            {
                //printf("%d %d\n",s[i].a,s[i].b);
                last=s[i].b;
                ans[i]=1;
                cas++;
                for(int j=i+1; j<n; j++)
                {
                    if(ans[j]==0&&s[j].a>last)
                    {
                        last=s[j].b;
                        ans[j]=1;
                    }
                }
            }
        }
        printf("%d\n",cas*10);
    }
    return 0;
}


nyoj 220 推桌子 poj 1083,布布扣,bubuko.com

nyoj 220 推桌子 poj 1083

原文:http://blog.csdn.net/y990041769/article/details/23738279

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!