The greedy (3) interval selecting problem of ACM knowledge points

Source: Internet
Author: User

Copyright NOTICE: This article is the main original article, reproduced must be annotated address: Http://blog.csdn.net/liuxucoder

The question of the interval point of choosing can be roughly described as:
given n intervals [a,b], take as few points as possible, so that there is at least one point in each interval (the points contained in different intervals can be duplicated).

About the greedy algorithm verification process is no longer to repeat, now think about the development of greedy strategy.
for intervals [A1, B1], [A2, B2], [A3, B3],
If you want to select the fewest points, you must select the right endpoint for each interval, as shown in the following diagram:

When you choose the right side of the interval each time, you can ensure that each selected point coverage is the widest range, which means that the selected point is the least.

Similar to the previous method of thinking, the interval is preprocessed, sorted by the size of the endpoint (again, sort by the right endpoint will understand a bit, but the left endpoint sort can play a role, beginners do not have to superstitious right endpoint sorting).
After preprocessing, the idea of the solution strategy is similar to that of the disjoint interval, if the left endpoint of the next interval is not overwritten, the answer +1 is as follows:

While (the number of remaining intervals is not 0)
{
    if (find the next interval that meets the criteria)
    {
        current interval = next interval; Number of
        answers +1
    }

    intervals--;
}

Read the question: Nyoj 1036

                                    African Children
                    time limit: 1000 ms  |  Memory limit: 65535 KB
                                    Difficulty: 2

Describe
The children who live in Africa are very dark. Why, then?
First, they are located in the tropics with severe solar radiation.
Second, they don't often bathe. (Perennial lack of water, how to bathe.) )
Now, in an African tribe, they have only one place to bathe, and the bath time is short, and there is wood in the moment. (This is also no way, lack of water ah.) )
Each child has a period of time to bathe. And, they can be washed together (whether you are a boy or a girl).
So, what time to bathe, who should come to wash, who decided. That must be their great "bath" God. "Bath" God has a timetable that records the children of the tribe, when the time can take a bath. Now, "bath" God wants to ask you, in one day, he needs to open and shut down how many times a shower faucet. Because, opening and closing a faucet once is very laborious, even, this is also instantaneous completes.

Input
Multiple sets of data
The first line is a n<=100.
Next n rows, one time period per line. H1H1:M1M1-H2H2:M2M2,24-hour system.
Ensure that the time period is within a day. However, there is no guarantee that h1h1:m1m1 before the h2h2:m2m2.

Output
The title describes how many taps the "shower" God needs to turn on and off at least once.

Sample input
1
00:12-12:12
2
00:12-12:12
14:00-12:00

Sample output
1
1

Tips
Ps: Turn on and off for once

The problem is the perfect interval, but there is a hole in the data (H1H1:M1M1 is not guaranteed to precede h2h2:m2m2.) So read into the time to pay attention to the judgment.

The resolution code is as follows:

* * ************************************ title:nyoj1036-African Child ************************************ DATE:2015/07/23 Author: Anthony Liu Xu ************************************ memory:256kb time:8ms ********* * #include <iostream> #include <cstdio> #include <cstring> #include <

Algorithm> using namespace std;
    #define MAX 1005 struct Node {int x;
int y;

};

Node Map[max];
    BOOL CMP (Node A,node b) {if (a.y!= b.y) {return a.y < b.y;
return a.x < b.x;
    int main () {int num = 0;
            while (EOF!= scanf ("%d", &num)) {memset (map,-1, sizeof (map));

            int a1,b1,a2,b2;
                for (int i = 0; i < num i++) {scanf ("%d:%d-%d:%d", &AMP;A1,&AMP;B1,&AMP;A2,&AMP;B2);
                int key1 = A1*60+B1;
                int key2 = A2*60+B2;
               if (Key1 > Key2) {swap (key1, key2); } map[i].x = Key1;
            Map[i].y = Key2;

            Sort (map, map+num, CMP);
            int start = MAP[0].Y;
            int num_node = 0;
            int ans = 1;
                    while (Num-num_node) {if (map[num_node].x > Start) {start = Map[num_node].y;
                ans++;
            } num_node++;
    printf ("%d\n", ans);
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.