1. 程式人生 > >Hdoj 3697 Selecting courses 【貪心】

Hdoj 3697 Selecting courses 【貪心】

enter 時間 hit size man style accepted 不能 ria

Selecting courses

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Others)
Total Submission(s): 2082 Accepted Submission(s): 543


Problem Description A new Semester is coming and students are troubling for selecting courses. Students select their course on the web course system. There are n courses, the ith course is available during the time interval (Ai
,Bi). That means, if you want to select the ith course, you must select it after time Ai and before time Bi. Ai and Bi are all in minutes. A student can only try to select a course every 5 minutes, but he can start trying at any time, and try as many times as he wants. For example, if you start trying to select courses at 5 minutes 21 seconds, then you can make other tries at 10 minutes 21 seconds, 15 minutes 21 seconds,20 minutes 21 seconds… and so on. A student can’t select more than one course at the same time. It may happen that no course is available when a student is making a try to select a course

You are to find the maximum number of courses that a student can select.


Input There are no more than 100 test cases.

The first line of each test case contains an integer N. N is the number of courses (0<N<=300)

Then N lines follows. Each line contains two integers Ai and Bi (0<=Ai
<Bi<=1000), meaning that the ith course is available during the time interval (Ai,Bi).

The input ends by N = 0.


Output For each test case output a line containing an integer indicating the maximum number of courses that a student can select.

Sample Input
2
1 10
4 5
0

Sample Output
2

題意:有n門課,選課時有以下rule:

1:每種課都有起始和結束,必須在之內選取。

2:每次選取之後5分鐘後不能再選課。

先依照結束時間從小到大排序,由於是每過五分鐘才幹夠選。那麽我們僅僅須要枚舉前四個時間,看是否在課程的起始與結束時間之內。就能夠了。

代碼:

#include <cstdio>
#include <cstring>
#include <algorithm>
const int M = 1100;
using namespace std;

struct node{
	int l, r;
}s[M];
int n;
bool vis[M];

bool cmp(node a, node b){
	return a.r < b.r;
}

int f(double x){
	memset(vis, 0, sizeof(vis));
	int res = 0;
	for(double d = x; d < M; d += 5){
		for(int i = 0; i < n; ++ i){
			if(!vis[i]&&d > s[i].l &&s[i].r > d){
				res++;
				vis[i] = 1; break;
			}
		}
	}
	return res;
}

int main(){
	while(scanf("%d", &n), n){
		for(int i = 0; i < n; ++ i) scanf("%d%d", &s[i].l, &s[i].r);
		sort(s, s+n, cmp);
		int ans = 0;
		for(double i = 0.5; i < 5; ++ i){
			ans = max(ans, f(i));
		}
		printf("%d\n", ans);
	}
	return 0;
} 


Hdoj 3697 Selecting courses 【貪心】