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
output
input
output
input
output
题意:给定n个人的到达时间和离开时间,问最多可以接待多少人。
思路:贪心,直接模拟就好了。
AC代码:
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;
}
相关文章推荐
- Codeforces 597A Divisibility 【数学计数】
- Codeforces 595B Pasha and Phone 【数学计数】
- Android源码阅读与理解(一):开篇
- A Bug's Life(并查集拓展)
- android面试攻略(2)
- 【Android】Android项目中R文件找不到怎么处理?
- 第十八章 并发登录人数控制
- Codility 网站练习题
- 推送消息
- 2)杨辉三角[1]队列实现
- Java学习中遇到的问题01_IO流
- Codeforces 595A Vitaly and Night 【水题】
- MyEclipse强制退出后闪退进不去workbench
- svn的服务器的配置
- 第十六章 综合实例
- 第十七章 OAuth2集成
- Java 声明变量名的规则
- 第十五章 单点登录
- 在64位Ubuntu 14.04 LTS 中安装praat
- 遍历一个文件夹下面的文件(半原创)