HDOJ3697 Selecting courses[贪心]
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.
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.
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门课的选课开始时间和截止时间,你可以从任意时间开始,每隔五分钟选一门课,问最多能选多少课。只能在间隔五分钟的那个时间点选课,如果选不了就只能等到下一个五分钟。(刚好在课程开放时间边界也是不能选的)
这相当与是用一堆间隔为5的叉子去叉,看能叉中多少个。只需从0-5枚举开始时间即可。给的时间都是整数,边界又不能选,可以从0.5到4.5来枚举,防止边界判断有误。
就按结束时间排个序,找的时候就找第一门在时间范围内且没被选的就好。
#include<iostream> #include<cassert> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<string> #include<iterator> #include<cstdlib> #include<vector> #include<stack> #include<queue> #include<map> #include<set> using namespace std; #define debug(x) cout<<"debug "<<x<<endl; #define rep(i,f,t) for(int i = (f),_end_=(t); i <= _end_; ++i) #define rep2(i,f,t) for(int i = (f),_end_=(t); i < _end_; ++i) #define dep(i,f,t) for(int i = (f),_end_=(t); i >= _end_; --i) #define dep2(i,f,t) for(int i = (f),_end_=(t); i > _end_; --i) #define clr(c, x) memset(c, x, sizeof(c) ) typedef long long int64; const int INF = 0x5f5f5f5f; const double eps = 1e-8; //***************************************************** int n; #define F first #define S second typedef pair<int,int> P; P a[330]; bool vis[330]; int maxt; int fn(double t){ rep(i,1,n){ if(vis[i])continue; if(a[i].F < t && t < a[i].S)return i; } return 0; } bool cmp(const P &p1,const P &p2){return p1.S < p2.S; } int xuan(double tm){ clr(vis,0); sort(a+1,a+n+1,cmp); int ans = 0; for(int i; tm < maxt; tm += 5){ if( i = fn(tm) ){ vis[i] = 1; ++ans; } } return ans; } int main() { a[0].S = INF; while(scanf("%d",&n), n){ maxt = 0; rep(i,1,n){ scanf("%d%d",&a[i].F,&a[i].S); if(a[i].S > maxt)maxt = a[i].S; } int ans = 0; for(double i = 0.5; i < 5; i += 0.5){ int tmp = xuan(i); ans = max(ans,tmp); } printf("%d\n",ans); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。