nyoj 220 推桌子 poj 1083


题意:

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


解法:贪心 或 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值