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

分析:经典的贪心题,记得贪心一点就能做对了~

代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

struct interval
{
    int x, y;

}itvl[100006], max_end[100006];

bool cmp(const interval &a, const interval &b)
{
    return a.x < b.x;
}

int main()
{
    int kase;
    cin >> kase;
    getchar();
    while (kase--){
        int ok = 0;
        int m,n=0;
        int cnt = 0;
        cin >> m;
        while (scanf("%d%d", &itvl[n].x, &itvl[n].y) && (itvl[n].x != 0 || itvl[n].y != 0)){
            ++n;
        }
        sort(itvl, itvl + n,cmp);

        int beg = 0, end = m;
        while (beg < end){
            max_end[cnt].y = 0;
            for (int i = 0; i < n; i++){
                if (itvl[i].x <= beg){
                    if (itvl[i].y>max_end[cnt].y)    {
                        max_end[cnt] = itvl[i];
                        ok = 1;
                    }
                }
                else{
                    break;
                }
            }
            if (ok == 0)        break;
            beg = max_end[cnt].y;
            cnt++;
        }

        //                        输出

        if (ok == 1){
            cout << cnt << endl;
            for (int i = 0; i < cnt; i++){
                cout << max_end[i].x << ‘ ‘ << max_end[i].y << endl;
            }
        }
        else{
            cout <<‘0‘<< endl;
        }
        if (kase != 0)            cout << endl;

    }
    return 0;
}
时间: 2024-10-04 13:19:56

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

可以说是区间覆盖问题的例题... 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 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

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

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

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(区间覆盖、贪心)

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

UVa 1647 - Computer Transformation 解题心得

这个题目.... 想上题意 10935 Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the to

UVa 10382 - Watering Grass 解题心得

原题: n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from the left end of t

UVA 673 Parentheses Balance 解题心得

原题 Description You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a) if it is the empty string (b) if A and B are correct, AB is correct, (c) if A is correct, (A ) and [A ] is correct. Write a pr