Nyoj_996_ Select disjoint intervals

Source: Internet
Author: User
Tags first row time limit
/*
Select non-intersecting intervals
Time limit: Ms | Memory Limit: 65535 KB
Difficulty: 2

Describe

All right. The aim of this problem is to let you know the three classical problems of greedy problem: interval selection problem, interval coverage problem, and the problem of choosing disjoint interval. There are a lot of greedy problems that can be translated into these three types of problems.

Well, for that matter. is to give a series of intervals, the most interval, requires the most interval number, these intervals do not intersect, it is important to note that these intervals are closed intervals.

Just for Fun

Input
First row one number n is the number of intervals (n<=1000)
Next there are n rows, each with two number, a, or two endpoints for the interval, and a, b in the int range.
EOF ends.
Output
Output as shown in the sample
Sample input

2
1 10
10 11
3
1 10
10 11
11 20

Sample output

Case 1:
1.
Case 2:
2.

Long time no algorithm, find some water problem practice practiced hand, small greedy.
There's a deceptive place to a<b. Swap the location of the input data.

*/

#include <stdio.h> #include <stdlib.h> struct node {int s;   Start int t;
END};
Node a[1001];
    int cmp (const void *a,const void *b) {node *c = (node *) A;
    Node *d = (node *) b;
    if (c->t>d->t) {return 1;
    } else {return-1;
    }} int main () {int n,i,ans,tmp;
    int num = 0;
        while (scanf ("%d", &n)!=eof) {if (n==0) {printf ("0\n"); } else {for (i=0;i<n;i++) {scanf ("%d%d", &a[i].s,&a[i].
                T);  
                if (a[i].s>a[i].t)//Pit a<b, swap the location of the input data.
                	{int tem = A[I].S;
                	A[I].S = a[i].t;
                a[i].t = tem;
            }} qsort (A,n,sizeof (a[0]), CMP);//From small to large sort tmp=0;
            Ans=1; for (i=1;i<n;i++) {if (a[i].s>a[tmp].t)//Take a second start and the first end comparison Greater than start plus, and place the position down {ans++;
                Tmp=i;
            }} printf ("Case%d:\n", ++num);
        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.