HDU 3697 Selecting courses(贪心+暴力)(2010 Asia Fuzhou Regional Contest)

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 (A i,B i). 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<=A i<B i<=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.

 

题目大意:给n个课程,每个课程有一个选择时间段(ai, bi),只能在这个时间段里面选择。然后你可以在任何时刻开始选课,然后每隔5分钟可以选一门课,问最多选多少门课。

思路:首先开始是越早越好,在8分开始不如在3分就开始。然后枚举5个开始时间,然后对每个时间枚举可以选择的课,选bi最小的(因为bi最小的被后面的时间选中的可能性最小,若bi能在后面被选中那么其他也能在后面被选中)。完全暴力不需要任何优化即可AC。

PS:那个(ai, bi)应该是开区间,不过我们可以选择在0分1秒的时候开始,所以可以看成是[ai, bi)。

 

代码(31MS):

 1 #include <cstdio>

 2 #include <iostream>

 3 #include <cstring>

 4 #include <algorithm>

 5 using namespace std;

 6 

 7 const int MAXN = 310;

 8 

 9 int a[MAXN], b[MAXN];

10 bool sel[MAXN];

11 int n;

12 

13 int solve() {

14     int ret = 0;

15     for(int st = 0; st < 5; ++st) {

16         memset(sel, 0, sizeof(sel));

17         int ans = 0;

18         for(int i = st; i <= 1000; i += 5) {

19             int best = -1;

20             for(int j = 0; j < n; ++j)

21                 if(!sel[j] && a[j] <= i && i < b[j]) {

22                     if(best == -1) best = j;

23                     else if(b[j] < b[best]) best = j;

24                 }

25             if(best != -1) {

26                 sel[best] = true;

27                 ++ans;

28             }

29         }

30         if(ret < ans) ret = ans;

31     }

32     return ret;

33 }

34 

35 int main() {

36     while(scanf("%d", &n) != EOF && n) {

37         for(int i = 0; i < n; ++i) scanf("%d%d", &a[i], &b[i]);

38         printf("%d\n", solve());

39     }

40 }
View Code

 

你可能感兴趣的:(select)