例题 电影节
每一部电影都有放映区间,区间重合的电影不能同时观看,(端点可以重合),问最多可以看多少部电影
输入:
n(n场<=100)
接下来n行 每行两个整数(<1000)表示放映区间
输出:
最多数量
eg:
输入
3
1 3
3 4
0 7
将所有电影按照结束大小从小到大排序,第一步选择结束最早的那部电影,然后每一步都选和上一步选中电影不冲突,并且结束时间最早的电影
只看眼前——>结束时间最早
不能按开始时间来排序:1—4 会排在 2—3前面,不正确。
#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;
}