Several kinds of interval coverage problems based on greedy algorithm--__ algorithm

Source: Internet
Author: User

(1) Interval full coverage problem
Problem Description:
Given an interval of length m, give the starting and ending points of n segments (note that this is a closed interval), and
ask for a minimum number of segments to completely cover the entire range
Example:
Interval length 8, optional overlay segment [2,6],[1,4],[3,6],[3,7],[6,8],[2,4],[3,5]
Problem Solving process:
1, each interval in accordance with the left endpoint increment order, after the sequence for [1,4],[2,4] , [2,6],[3,5],
[3,6],[3,7],[6,8]
2, set a variable to represent the area that has been overwritten. The remaining segments are found in segments that are less than the right endpoint of the current
already covered area. The largest segment of the right endpoint is joined until the entire area has been overwritten
3, procedure:
Assuming the first step is to join [1,4], then the next option is to have [2,6],[ 3,5],[3,6],[3,7],
because the 7 is the largest, so next select [3,7], the last step can only choose [6,8], this time just
reached 8 exit, the selection between 3
4, greedy proof:
requires a minimum of segments to cover, Then the selected segment must be as long as possible, it doesn't matter where the
is already covered, but it's understandable that all the left endpoints that can be covered are already covered,
so it really makes the segment more of the right endpoint and the left endpoint doesn't make much sense. So select the right endpoint to overwrite the

(2) Maximum disjoint overlay
Problem Description:
Given an interval of length m, and then give the starting and ending points of the n segment (open interval and closed interval processing is different, for example, in open interval), The problem is to select as many segments as possible, so that each segment is independent, and
is not where any of the other segments Intersect
Sample:
Interval length 8, optional overlay segment [2,6],[1,4],[3,6],[3,7],[6,8],[2,4],[ 3,5]
Problem solving process:
To sort the right endpoint of a line in ascending order, each join a line segment, and then select a number of the following
(and possibly a) right endpoint of the same segment, select the largest left endpoint, if the join will not
with the previous line to generate a common part, Then join, or you will continue to judge the following line segments
1, sorting: Each interval by the right endpoint for ascending order, after the completion of the sequence for [1,4],[2,4],[3,5],[2,6],
[3,6],[3,7],[6,8]
2, the first step selection [ 2,4], found that the following can only be added [6,8], so the number of intervals is 2
3, greedy proof: Because the need for as many independent segments, so each segment is as small as possible,
for the same right endpoint, the larger the left endpoint, the smaller the length of the segment. So why do you want to sort the right endpoint?
If the left endpoint is sorted, then the right endpoint is not known, then each segment cannot be summed up for all previous
segments, so this obviously does not satisfy the greedy optimal substructure.

(3) interval selecting points
Problem Description:
Given an interval of length m, we give the requirement of n segment and n line segment to satisfy
(requirement is the number of at least one selected point on the n segment), the problem is the least
Select a few points to meet the requirements of each line segment.
Examples: slightly
Problem Solving Process:
Increments each segment by the endpoint coordinate, and the front point coordinates of the same endpoint are arranged from large to small,
Meet one at a time (each selected point is the right end of the segment)
Greedy Proof:
To make the rest of the line select the least points, you should try to make the selected points as far as possible
To play a role in the following segment, and we select the segment from left to right, so that the selected point can
To meet the requirements of the back segment, you must start the point from the right end of the line, so the problem (2) involves
To a problem, if the line is sorted by the left endpoint of the line, do not know the right endpoint,
Each line segment does not have a summary of all the segments that have been previously manipulated, and this is equally unsatisfied
The optimal substructure property of greedy algorithm.
The practical problem that can be solved: the axis has n closed intervals [a,b], taking as few points as possible, so that each interval is
At least one point (the dots may be the same in different intervals)

Interval Selecting algorithm implementation:

#include <iostream>
#include <algorithm>

using namespace std;

struct line
{
    int left;
    int right;
} A[100];

BOOL CMP (line p, line Q)
{
    if (p.right!= q.right) return
        p.right < q.right;
    return p.left > Q.left;
}

int main ()
{
    int n;
    while (CIN >> N)
    {for
        (int i = 0; i < n; ++i)
            cin >> a[i].left >> a[i].right;
        Sort (A, a + N, CMP);
        int cnt = 0;
        int end =-1;
        for (int i = 0; i < n; ++i)
        {
            if (end >= a[i].left && end <= a[i].right)
                continue;
            else
            {
                ++cnt;
                end = A[i].right;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.