http://acm.hdu.edu.cn/showproblem.php?pid=3697
Selecting courses
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Others)
Total Submission(s): 3334 Accepted Submission(s): 948
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 (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.
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.
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.
Sample Input
2 1 10 4 5 0
Sample Output
2
思路:因为(x,y)是开区间,每分钟选一个整数即可,所以选每个分钟的开始,所以(2,5)右(要左不要)包含了2,3,4,不包含5.
分为还有0.5.10.15.。。。995,还有1,6,11,16,21.......996这种,还有2,3,4开头的等等
用数组a[i][j]存一下,包含i这一分钟的第j个区间的编号。
每个编号都有这个区间的信息,用结构体存一下,开一个flag标记一下这个区间是否被选过。
然后找区间用贪心,现在找一个包含23分钟的区间,那么我去a[23][]里找一个y最小,并且没有被选的区间即可(flag=1),每次排一下序,直接选排在最前边的区间即可。
代码:
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
using namespace std;
struct node
{
int x,y,flag;
} b[305];
int a[1005][305];
bool cmp(int q,int qq)//q和qq是区间的编号
{
if(b[q].flag==1&&b[qq].flag==1)//首先看标记,都没被选过就比较y,小的在前
return b[q].y<b[qq].y;
else return b[q].flag>b[qq].flag;//没被标记的排在前
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
for(int i=0; i<=1000; i++)
a[i][0]=0;//a[i][0]表示a[i]里存的区间编号的个数
int p,q;
for(int i=1; i<=n; i++)
{
scanf("%d%d",&p,&q);
b[i].x=p;
b[i].y=q;
b[i].flag=1;//初始化为1,表示未被标记
for(int j=p; j<q; j++)//区间(p,q)包含p,p+1....q-1;
a[j][++a[j][0]]=i;
}
int ans=0;
for(int i=0; i<5; i++)//枚举开头
{
for(int j=1;j<=n;j++)
b[j].flag=1;//标记清空为1
int w=0;
for(int j=i; j<=1000; j+=5)
{
if(!a[j][0]) continue;//判断是否有包含j分钟的区间
sort(a[j]+1,a[j]+a[j][0]+1,cmp);//排序,把最优的区间排在第一个
if(b[a[j][1]].flag==1)//如果第一个区间没有被标记的话,就选这个区间
{
w++;//统计数量
b[a[j][1]].flag=0;//标记一下
}
}
ans=max(ans,w);
}
printf("%d\n",ans);
}
}