您的位置:首页 > 其它

Codeforces 597B Restaurant 【贪心】

2015-11-13 21:56 387 查看
B. Restaurant

time limit per test
4 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th
order is characterized by two time values — the start time li and
the finish time ri (li ≤ ri).

Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?

No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.

Input

The first line contains integer number n (1 ≤ n ≤ 5·105)
— number of orders. The following n lines contain integer values li and ri each
(1 ≤ li ≤ ri ≤ 109).

Output

Print the maximal number of orders that can be accepted.

Sample test(s)

input
2
7 11
4 7


output
1


input
5
1 2
2 3
3 4
4 5
5 6


output
3


input
6
4 8
1 5
4 7
2 5
1 36 8


output
2


题意:给定n个人的到达时间和离开时间,问最多可以接待多少人。

思路:贪心,直接模拟就好了。

AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (500000+10)
#define MAXM (50000000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000003#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1#define rr o<<1|1using namespace std;
struct Node{
int s, t;
};
Node num[MAXN];
bool cmp(Node a, Node b){
return a.t < b.t;
}
int main()
{
int n; Ri(n);
for(int i = 0; i < n; i++)
Ri(num[i].s), Ri(num[i].t);
sort(num, num+n, cmp);
int ans = 0, pre = num[0].t;
for(int i = 1; i < n; i++)
{
if(num[i].s <= pre)
ans++;
else
pre = num[i].t;
}
Pi(n-ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: