Greedy Training Problems

Interval greedy
given a series of intervals, so that you make arrangements;
this question given a series of intervals, seeking the most number of individual sections, with the first pair for storage or struct, and then customize the sort of interval point have the right sort
then int t = 0, t is compared with the front end point, the front end point was less independent interval, t becomes the right end of the current point

Explanation:
for the end time of ordering reason is because, in the end must arrange the earliest, if the latter appeared in the earliest end time re-
fit, no matter what that may be behind
such as
14 and 24
obviously the second time is short, but the election the first and second are the same
if there is an end time shorter, then the first one is a shorter one.

But if the need to protect the longest time, then it would require other conditions, and this is only to obtain maximum number of individual sections

4
1 3
4 6
2 5
1 7

Sequence

. 3. 1
2. 5
. 4. 6
. 1. 7
and t = 0;
traversing,
T <=. 1,. 1 = the ANS, T =. 3;
T> 2, regardless of the number of second
t <= 4, ans = 2 , t . 6 =
T>. 1, regardless of the number of the fourth

A title and the title H

Topic links:
https://ac.nowcoder.com/acm/contest/950/A
https://ac.nowcoder.com/acm/contest/950/H

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1005;

bool cmp(pair<int,int>a,pair<int,int>b){
    return a.second < b.second;
}
int main(){
    int n;
    while(scanf("%d",&n)!=EOF&&n){
        pair<int,int>g[maxn];
        for(int i=0;i<n;i++){
            scanf("%d%d",&g[i].first,&g[i].second);
        }
        sort(g,g+n,cmp);//对pair的排序,默认是对first排序

        int t=0,ans=0;
        for(int i=0;i<n;i++){
            if(g[i].first>=t){
                t=g[i].second;
                ans++;
            }
        }
        
        printf("%d\n",ans);


    }

    return 0;
}

Guess you like

Origin www.cnblogs.com/Emcikem/p/11333807.html