码迷,mamicode.com
首页 > 其他好文 > 详细

[贪心] poj 2376 Cleaning Shifts

时间:2015-05-07 14:28:20      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:

题意:

给n个线段,问你覆盖满[1,T]内的每个点,最少需要多少条线段。

思路:

贪心,按照x坐标升序,y坐标降序排。

默认先放入第一条。

然后根据从属关系继续放线段。

注意的一点是 [1,2]和[3,4]就可以覆盖满[1,4]了。

然后注意因为默认放入了第一条,所第一条的状态要特判。

包括 第一条如果是[1,T]的话 输出1.

代码:

#include"cstdlib"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"queue"
#include"algorithm"
#include"iostream"
#include"map"
using namespace std;
struct node
{
    int x,y;
}p[26000];
int cmp(node a,node b)
{
    if(a.x==b.x)
    {
        if(a.y>b.y) return 1;
        return 0;
    }
    if(a.x<b.x) return 1;
    return 0;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y);
        sort(p,p+n,cmp);
        if(p[0].x!=1)
        {
            puts("-1");
            continue;
        }
        int xx=0;
        int yy=p[0].y;
        int ans=1;
        for(int i=1;i<n;i++)
        {
            if(p[i].y<yy) continue;
            if(yy>=m) break;
            if(p[i].x>yy+1)
            {
                ans=-1;
                break;
            }
            if(p[i].x>xx+1)
            {
                ans++;
                xx=yy;
                yy=p[i].y;
            }
            else
            {
                yy=p[i].y;
            }
        }
        if(yy<m) ans=-1;
        printf("%d\n",ans);
    }
    return 0;
}


[贪心] poj 2376 Cleaning Shifts

标签:

原文地址:http://blog.csdn.net/wdcjdtc/article/details/45560419

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!