Greedy topic - Film Festival

Examples Film Festival

Every film has screened interval, the interval of overlap can not watch a movie at the same time, (the endpoint may coincide), asked how many movies you can watch up to
enter:
n (n field <= 100)
followed by two integers n lines per row ( <1000) show interval represents
output:
the largest number

eg:
Input
3
1 3
3 4
0 7
all in accordance with the end of the movie size from small to large, the first step in selecting the end of the first that movie, and then choose each step and the last step select the movie does not conflict, and the earliest end time film
is for the present -> end the earliest time
can not be sorted by start time: 1-4 2-3 in the front row will be incorrect.

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#define M  100010
#define INF 0x3f3f3f3f
const double EPS = 1e-6;
using namespace std;
struct movie{
    int begin,end;
    bool operator < (const movie &other)const{
        return end < other.end;
    }
}a[M];
int main(){
    int n;
    cin >> n;
    for(int i=0; i<n; i++)
        cin >> a[i].begin >> a[i].end;
    sort(a,a+n);
    for(int i=0; i<n; i++)
        cout <<  a[i].begin << " " <<  a[i].end << endl;
    int now=0,cnt=0;
    for(int i=0; i<n; i++){
        if(a[i].begin>=now){
            now = a[i].end;
            cnt++;
        }
    }
    cout << cnt << endl;
    return 0;
}

Published 62 original articles · won praise 0 · Views 1745

Guess you like

Origin blog.csdn.net/jhckii/article/details/104445480