P1803 凌乱的yyy / 线段覆盖(贪心)

题目背景

快noip了,yyy很紧张!

题目描述

现在各大oj上有n个比赛,每个比赛的开始、结束的时间点是知道的。

yyy认为,参加越多的比赛,noip就能考的越好(假的)

所以,他想知道他最多能参加几个比赛。

由于yyy是蒟蒻,如果要参加一个比赛必须善始善终,而且不能同时参加2个及以上的比赛。

输入输出格式

输入格式:

第一行是一个整数n ,接下来n行每行是2个整数ai,bi(ai<bi),表示比赛开始、结束的时间。

输出格式:

一个整数最多参加的比赛数目。

输入输出样例

输入样例#1: 复制

3
0 2
2 4
1 3

输出样例#1: 复制

2

说明

对于20%的数据,n≤10;
对于50%的数据,n≤1000;
对于70%的数据,n≤100000;
对于100%的数据,n≤1000000,0≤ai<bi≤1000000。

类似活动选择,按结束时间从小到大排序,然后如果前一个的结束时间小于等于后一个的开始时间,ans++ 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <set>
#include <cstring>
#include <stack>
#include <set>
#include <vector>
#include <queue>
#define Swap(a,b)  a ^= b ^= a ^= b
#define cl(a,b) memset(a,b,sizeof(a))
using namespace std ;
typedef long long LL;
const int N = 1e7+10 ;

LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
const int MAX = 1000005;
const int inf = 0xffffff;
const LL mod = 1e9+7 ;
priority_queue<int,vector<int>,greater<int> > q ;// 小跟堆
priority_queue<int> Q ; // 大堆
struct node{
	int s ; // 开始时间
	int e ; // 结束时间
}a[MAX];
bool cmp(const node &a , const node &b){
	return a.e < b.e;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    int n ;
    cin >> n;
    for(int i = 1 ; i<=n ; i++ ){
		cin >>a[i].s >>a[i].e;
	}
	sort(a+1,a+1+n,cmp);
	int ans = 0 ;
	int t = 0 ;
	for(int i = 1 ; i<=n ;i++){
		if(t<=a[i].s ){
			ans++;
			t = a[i].e ;
		}
	}
	cout<<ans;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41661809/article/details/86678307