HDU 2037 this summer not AC (greedy)

Source: Internet
Author: User
B-not AC this summer holiday crawling in process ... crawling failed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64D &%i64u Submitstatus

Description "This summer holiday is not AC. ”
Yes ”
"Then what are you doing?" ”
"Watch the World Cup, idiot." ”
"@#$%^&*% ..."

Indeed, the World Cup has come, the fans of the festival has come, it is estimated that many acmer will also throw away the computer, to the TV.
As fans, must want to see as much as possible complete game, of course, as a new era of good youth, you will certainly see some other programs, such as news broadcast (never forget about the state of affairs), very 6+7, Super girls, and Wang Xiao Ya's "Happy Dictionary" and so on, If you already know the schedule of all the TV shows you like to watch, will you make reasonable arrangements? (The goal is to be able to see as many full programs as possible)

Input data contains multiple test instances, and the first line of each test instance has only one integer n (n<=100), representing the total number of shows you like to see, followed by n rows of data, each row containing two data ti_s,ti_e (1<=i<=n), Represents the start and end times of the I program, respectively, in order to simplify the problem, each time is represented by a positive integer. N=0 indicates that the input is finished and not processed.

Output for each test instance, outputs the number of TV shows that can be fully seen, one row for each test instance output.

Sample Input

12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 0

Sample Output

5 The most typical greedy, today just learn the small white inside the greedy algorithm, just corresponds to the inside of the choice of disjoint interval problem. The interval is sorted by the right endpoint from small to large, then the first interval is selected, and the rest is scanned, because it has been sorted before, as long as it does not intersect with the previous interval to be selected.
#include <stdio.h>
#include <algorithm>
using namespace std;
struct Node
{
    int st,en;
} NODE[105];
BOOL CMP (struct node a,struct Node B)
{
    return a.en<b.en;
}
int main ()
{
    int n,ans;
    while (~SCANF ("%d", &n)
    } {
        if (n==0) break
            ;
        for (int i=0; i<n; i++)
            scanf ("%d%d", &node[i].st,&node[i].en);
        Sort (node,node+n,cmp);
        The first interval of ans=1;//must be selected
        int pos=node[0].en;
        for (int i=1; i<n; i++)
        {
            if (Node[i].st>=pos)
            {
                ans++;
                Pos=node[i].en;
            }
        }
        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.