TOJ 2894. Meetings(贪心基础)

题目链接:http://acm.tju.edu.cn/toj/showp2894.html


2894.    Meetings
Time Limit: 3.0 Seconds    Memory Limit: 65536K
Total Runs: 2042    Accepted Runs: 599



There are several meeting rooms in Tianjin University. Since there are a lot of new students registering at the beginning of the term, the directors of school have scheduled many meetings to discuss upcoming issues. Suppose there are N meetings in total. The i-th meeting starts at time si and ends at time ei. Consider the j-th meeting can be held immediately after the i-th meeting in the same meeting room. Then we must have sj ≥ ei. In order to minimize usage of resources, the directors of school has asked you to write a program that calculates the minimum number of meeting rooms required for the whole meetings.

Input

There are several test cases in the input data. The first line contains the number of test cases. The first line contains a positive integer N (1 ≤ N ≤ 105) denoting the number of meetings. Then there are N lines, and the i-th line contains si and ei, (1 ≤ si < ei ≤ 109) denoting the start time and the end time of i-th meeting.

Output

Output the minimum number of meeting rooms required for the whole meetings.

Sample Input

2
2
1 3
3 5
3
3 5
1 4
4 5

Sample Output

1
2

Hint: In the second sample input, we can arrange the meeting like this to minimize the number of meeting rooms required:
Room A: (1, 4) (4, 5)
Room B: (3, 5)



Source: TJU Exam 2007
Submit   List    Runs   Forum   Statistics

贪心,蛮不错的一道题,好好理解一下:

#include <stdio.h>
#include <algorithm>
using namespace std;
#define Max 100001
#define INF 1<<29
int s[Max],e[Max];
int main(){
	int cast,n;
	scanf("%d",&cast);
	while(cast--){
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%d%d",&s[i],&e[i]);
		sort(s,s+n);
		sort(e,e+n);
		int sum=0;
		for(int i=0,j=0;i<n;i++)
			if(s[i]<e[j])
				sum++;
			else
				j++;
		printf("%d\n",sum);		
	}
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值