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].
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.
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

题意:给定一个M,和一些区间[L,R]。。要选出几个区间能完全覆盖住[0,M]区间。要求数量最少。。如果不能覆盖输出0.

思路:贪心的思想。。把区间按R从大到小排序。 然后遇到一个满足的[Li,Ri],就更新缩小区间。。直到完全覆盖。

注意[L,R]只有满足L小于等于且R大于当前覆盖区间左端这个条件。才能选中。

#include <stdio.h>
#include <algorithm>
using namespace std;
int t;
int start,end,n,f;
struct qujian
{
    int start;
    int end;
};
qujian q[100005],p[100005];
int cmp (qujian a,qujian b)
{
    return a.end > b.end;
}
int main()
{
    scanf("%d", &t);
    while (t --)
    {
        n = 0;
        f = 0;
        start = 0;
        scanf("%d", &end);
        while (scanf("%d%d", &q[n].start, &q[n].end) && q[n].start + q[n].end)
        {
            n++;
        }
        sort(q,q+n,cmp);
        while(start<end)
        {
            int i;
            for (i=0;i<n;i++)
            {
                if (q[i].start <= start && q[i].end > start)
                {
                    start = q[i].end;   //更新区间
                    p[f] = q[i];
                    f++;
                    break;
                }
            }
            if (i==n) break;   //如果没有一个满足条件的区间,直接结束。
        }
        if(start<end) printf("0\n");
        else
        {
            printf("%d\n",f);
            for (int i=0;i<f;i++)
                printf("%d %d\n", p[i].start,p[i].end);
        }
        if (t) printf("\n");
    }
    return 0;
}
时间: 2024-10-24 13:12:03

UVa 10020 - Minimal coverage(区间覆盖并贪心)的相关文章

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

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

算法入门经典关于区间覆盖的讲解: 8.4.6:区间覆盖问题 数轴上有n个区间[ai,bi],选择尽量少的区间覆盖一条指定线段[s,t]. [分析] 突破口是区间的包含和排序扫描,不过要先进行一次预处理.每个区间在[s,t]外的部分应该首先被切除掉,因为他们的存在是没有意义的.在预处理后,在相互包含的情况下,小区间显然不应该考虑. 把区间按照a从小到大排序.如果区间1的七点不是s,无解,否则选择起点在s的最长区间.选择此区间[ai,bi]后,新的起点应该设置为bi,并且忽略所有区间在bi之前的部分

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

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

【区间覆盖问题】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

ural Minimal Coverage (区间覆盖)

http://acm.timus.ru/problem.aspx?space=1&num=1303 给出一些区间,选择尽量少的区间能覆盖到[0,m]. 小白p154,典型的区间覆盖问题.一直在想怎么dp.. 首先预处理,先按左端点从小到大排序,若左端点相同右端点从大到小排序,若区间x完全包含y,按照贪心的思想,y是没有意义的,有大区间可以选何必选择小区间.处理完事之后各个区间满足a1 <= a2 <= a3....且b1 <= b2 <= b3... 这样找到第一个覆盖0的

UVa 10020 Minimal coverage

题意是:输入几个样例,每个样例第一行输入从0开始需要覆盖的长度M,即[0,M].之后输入覆盖的线段,求需要的线段条数最小值. 思路:贪心算法,具体见代码及注释. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; #define MAXN 100001 /*将输入数

UVA 10020 - 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

UVA 10382 Watering Grass(区间覆盖,贪心)题解

题意:有一块草坪,这块草坪长l 米,宽 w 米,草坪有一些喷头,每个喷头在横坐标为 p 处,每个喷头的纵坐标都是(w/2) ,并且喷头的洒水范围是一个以喷头为圆心,半径为 r 米的圆.每次最少需要打开多少个喷头来给草坪洒水,并且草坪各处都能被洒到,不行输出-1 思路:这是一道区间覆盖(贪心)题: 有一堆区间 l1, r1:l2, r2...ln,rn,问你最少用几个能覆盖0~P的长度 那么我们先按照L升序排序,far是目前所能找到的最远处,R是上一次查询所能找到的最远处,每次查询我们都要找后面满

【贪心专题】POJ 1328 G - Radar Installation (区间覆盖)

链接:click here~~ 题意: Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only