"Algorithmic learning note" 51. Interval sorting problem SJTU OJ 1360 idol Ding-ting's troubles

Source: Internet
Author: User

Description

Become the LL champion of popular idol Ding sister recently annoyed, many business activities to find the door. Because every business activity to give the same grandpa Mao, so sister Ding want to be able to participate in these activities as much as possible. However, the start and end time of business activities is not the boss, so sister Ding wanted to write a program to find out the maximum number of business activities he can participate in.

Input Format

The first row is a number n, which represents the number of optional activities.

The n rows are followed by two numbers per line, indicating the time t2_i for each activity start time t1_i and end.

Output Format

A number that indicates the maximum number of activities that sister Ding can participate in.

Sample Input
100 30 510 1312 152 64 89 1113 1814 1615 20
Sample Output
5
Hint

Examples of selected activity times are: (0, 3), (4, 8), (9, 11), (12, 15), (15, 20)

n≤100000

0≤t1_i

train of thought 1. Sort by left (same left press right)The error code is as follows:
#include <iostream>#include<algorithm>using namespacestd;structperiod{intstart; intend;}; //Sort by StartintCmp_period (Const void* _a,Const void*_b) {Period* A = (period*) _a; Period* B = (period*) _b; if((*a). Start! = (*b). Start)return(*a). Start-(*b). Start; Else        return(*a). End-(*b). End;} Period ps[100001]; intMainintargcChar Const*argv[]) {        intN; CIN>>N;  for(inti =0; I < n; ++i) {cin>>ps[i].start>>Ps[i].end; } qsort (Ps,n,sizeof(Period), cmp_period); //for (int i = 0; i < n; ++i)// {    //cout<<ps[i].start<< "" <<ps[i].end<<endl; // }        intCur = ps[0].start; intindex =0; intAns =1;  while(1) {cur=Ps[index].end; Index++;  for(; index < n; index++){            if(Ps[index].start >=cur) {                if(index<n-1and ps[index+1].end <ps[index].end) Index++; Ans++;  Break; }        }        if(Index >= N1)             Break; } cout<<ans<<Endl; return 0;}
error code 1when the sample is sorted, it is0 30 52 64 89Ten the All from theThe first instinct is to select the first interval and then select the first interval that is greater than or equal to the right end of the first interval (4,8) and so on. Because the same starting point, the choice is the tail of the smaller. But the problem with this approach is that there is no way to extend a complete overlap interval. like0 33 94 88 9Choose 0 3, 3 9 but actually 0 3, 4 8, 8 9, according to the algorithm you just madeThe reason is that 4,8 is completely contained in the 3,9, so we have to choose 4,8 and go on.So we have to make changes on this basis. (Alternatively, you can temporarily discard the idea of skipping all the same head ...) Note that the part of the core calculation should be an on-line algorithm (O (n)) This also implies that whenever you stop, you should get the current results first get 0,3 and then update to 3 92 then another 4 8 we're going to see the relationship between 4 8 and 3 9 first 4>=3 is certain because of the sort, So just determine the size of the two end. found that if the tail of the new element is less than the end of the old element, you can delete 3 9 to change to 4 8 because the deletion of one increment so that ans does not change, but the tail to be updated to 8 so only need to maintain a variable cur can represent the tail of the current results of the number. (In fact, this is implied, by the tail sort more convenient, the maintenance process is actually the process of sorting the tail.) The correct code is as follows:
#include <iostream>#include<algorithm>using namespacestd;structperiod{intstart; intend;}; //sort by starting pointintCmp_period (Const void* _a,Const void*_b) {Period* A = (period*) _a; Period* B = (period*) _b; if((*a). Start! = (*b). Start)return(*a). Start-(*b). Start; Else        return(*a). End-(*b). End;} Period ps[100001]; intMainintargcChar Const*argv[]) {        intN; CIN>>N;  for(inti =0; I < n; ++i) {cin>>ps[i].start>>Ps[i].end; } qsort (Ps,n,sizeof(Period), cmp_period); intCur = ps[0].end; intAns =1;  for(inti =1; I < n; ++i) {if(Ps[i].start >=cur) {cur=Ps[i].end; Ans++; }        if(Ps[i].end <cur) cur=Ps[i].end; } cout<<ans<<Endl; return 0;} 
idea 1 Correct code

Idea 2: Sort by right side, directly traverse the election of all the nodes with the start time greater than the current termination time, and continuously maintain the update termination time.

In the words of the gun brother, this is actually a greedy strategy, the earlier the end of the description can participate in other activities earlier, so to the end of the order.

The code is simple, as follows:

#include <iostream>#include<algorithm>using namespacestd;structperiod{intstart; intend;};//Sort by EndintCmp_period (Const void* _a,Const void*_b) {Period* A = (period*) _a; Period* B = (period*) _b; //if ((*a). End! = (*b). End)        return(*a). End-(*b). end; //Else//return (*a). Start-(*b). Start;}period ps[100001];intMainintargcChar Const*argv[]) {        intN; CIN>>N;  for(inti =0; I < n; ++i) {cin>>ps[i].start>>Ps[i].end; } qsort (Ps,n,sizeof(Period), cmp_period); intAns =1; intCur = ps[0].end;  for(inti =1; I < n; ++i) {if(Ps[i].start >=cur) {cur=Ps[i].end; Ans++; }} cout<<ans<<Endl; return 0;}/*510 1312 1113 1814 1615*/
idea 2 correct code

"Algorithmic learning note" 51. Interval sorting problem SJTU OJ 1360 idol Ding-ting's troubles

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.