UVA Minimal coverage (贪心)

Description

 Minimal coverage 

The Problem

Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M].

The Input

The first line is the number of test cases, followed by a blank line.

Each test case in the input should contains an integer M(1<=M<=5000), followed by pairs "Li Ri"(|Li|, |Ri|<=50000, i<=100000), each on a separate line. Each test case of input is terminated by pair "0 0".

Each test case will be separated by a single line.

The Output

For each test case, in the first line of output your programm should print the minimal number of line segments which can cover segment [0,M]. In the following lines, the coordinates of segments, sorted by their left end (Li), should be printed in
the same format as in the input. Pair "0 0" should not be printed. If [0,M] can not be covered by given line segments, your programm should print "0"(without quotes).

Print a blank line between the outputs for two consecutive test cases.

Sample Input

2

1
-1 0
-5 -3
2 5
0 0

1
-1 0
0 1
0 0

Sample Output

0

1
0 1

Alex Gevak

September 10, 2000 (Revised 2-10-00, Antonio Sanchez)

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct node
{
    int l;
    int r;
} q[100100];

int cmp(const void *a,const void *b)
{
    struct node *aa,*bb;
    aa = (struct node*)a;
    bb = (struct node*)b;
    return aa->l - bb->l;
}

int main()
{
    int T;
    int n;
    scanf("%d",&T);
    while(T--)
    {
        int m;
        scanf("%d",&m);
        n = 0;
        while(scanf("%d%d",&q[n].l,&q[n].r)!=EOF)
        {
            if(q[n].l == 0 && q[n].r == 0)
            {
                break;
            }
            n++;
        }
        qsort(q,n,sizeof(q[0]),cmp);
        int pmax = 0;
        int pi = 0;
        int ff = 0;
        for(int i=0; i<n; i++)
        {
            if(q[i].l<=0 && q[i].r>pmax)
            {
                ff = 1;
                pmax = q[i].r;
                pi = i;
            }
            else if(q[i].l>0)
            {
                break;
            }
        }
        if(ff == 0)
        {
            printf("0\n");
            continue;
        }
        int left = 0;
        int right = pmax;
        int maxx = 0;
        int a[10010];
        int k = 0;
        int p = 0;
        int h = 0;
        int flag = 0;
        a[p] = pi;
        p++;
        for(int i=0; i<n; i++)
        {

            if(q[i].l<=right && q[i].r>maxx)
            {
                if(flag == 0)
                {
                    right = q[i].r;
                    flag = 1;

                }

                maxx = q[i].r;
                h = i;
                if(maxx >=m)
                {
                    if(h!=a[p-1])
                    {
                        a[p] = h;
                        p++;
                    }
                    break;
                }
            }
            else if(q[i].l>right && right!=0)
            {
                right = q[h].r;
                if(h!=a[p-1])
                {

                    a[p] = h;
                    p++;
                }

                if(q[i].r>maxx)
                {
                    maxx = q[i].r;
                    h = i;
                    if(i == n-1)
                    {
                        a[p] = h;
                        p++;
                    }
                }
            }
        }

        if(p == 0)
        {
            printf("0\n");
        }
        else
        {
            printf("%d\n",p);
            for(int i=0; i<p; i++)
            {
                printf("%d %d\n",q[a[i]].l,q[a[i]].r);
            }
        }
        if(T)
        {
            printf("\n");
        }
    }
    return 0;
}
时间: 2024-11-05 12:23:19

UVA Minimal coverage (贪心)的相关文章

uva 10020 Minimal coverage(贪心,区间覆盖)

这道题一读就是经典的区间问题,是区间覆盖,敲过之后还有花了很长的调试时间,还是我不熟练,现在做题确实挺慢 的,简单题目也要做好久,没事,慢慢来.最重要的要确保正确率和心态问题,认真对待,调试找到了好多bug,一些 细节问题...都是刚开始没有注意到的.交了之后RE,在数组上多加了两个0.A了,,uva老是不提示数据有多大, 所以只能乱开... 思路: 先对区间按左边的点进行排序,如果当前需要涵盖的区间为[x,y],那么在排序的区间中到左边小于x,右 边最大的那个区间,设为Max,然后更新想找的区

uva 10020 Minimal coverage 【贪心】+【区间完全覆盖】

Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M]. The Input The first line is the number of test ca

贪心 URAL 1303 Minimal Coverage

题目传送门 1 /* 2 题意:最少需要多少条线段能覆盖[0, m]的长度 3 贪心:首先忽略被其他线段完全覆盖的线段,因为选取更长的更优 4 接着就是从p=0开始,以p点为标志,选取 (node[i].l <= p && p < node[i+1].l) 5 详细解释:http://www.cnblogs.com/freezhan/p/3219046.html 6 */ 7 #include <cstdio> 8 #include <iostream>

uva 10020- Minimal coverage (贪心思想 简单区间覆盖)

题目大意:给出一个范围M,然后给出若干的区间,以0 0 终止, 要求用最少的区间将0 ~M 覆盖,输出最少个数以及方案. 解题思路:典型的区间覆盖问题,算法竞赛入门经典P154上有讲. /*author: charkj_z */ /*time: 0.108s */ /*rank: 674 */ /*为什么不把没用的地方去掉? 因为去掉了我觉得不像我能写出来的*/ /*Ac code : */ #include<stdio.h> #include<string.h> #include

Timus 1303 Minimal Coverage DP或贪心

1303. Minimal Coverage Given set of line segments [Li, Ri] with integer coordinates of their end points. Your task is to find the minimal subset of the given set which covers segment [0, M] completely (M is a positive integer). Input First line of th

【区间覆盖问题】uva 10020 - Minimal coverage

可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选择起点在s的最长区间; 2.选择区间[li, ri]后,新的起点应更新为ri,并且忽略所有区间在ri之前的部分:  Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinat

uva 1511 - Soju(贪心)

题目链接:uva 1511 - Soju 题目大意:给出两个点集,问说分别从两个点集中取一点的哈夫曼距离最小值.注意一个点集的x坐标小于0,另一个大于0. 解题思路:因为x2一定大于x1,所以对于x这一维,一定是+x2-x1,所以只需要考虑y这一维坐标即可. #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> u

uva10020 - Minimal coverage(区间覆盖)

题目:uva10020 - Minimal coverage(区间覆盖) 题目大意:给出一些线段,然后问怎样取能使得最少的线段覆盖区间[0, M]. 解题思路:先预处理掉那些和区间[0,M]不沾边的线段. 将线段按照起点小的排序. 接着遍历这些线段.首先先判断起点最小的点是否<=0,如果不满足这个说明它不能覆盖区间. 然后每次都小于等于当前覆盖的起点的最长线段,之后要将起点更新成这个最长线段的终点.然后接着判断下一条线段.如果更新了起点发现依然找不到满足条件的线段,说明不能覆盖. 最后还要看一下

UVa 10020 - Minimal coverage(区间覆盖并贪心)

Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0, M].InputThe first line is the number of test cases, followed by a blank line.Eac