首页 > ACM题库 > HDU-杭电 > HDU 3697-Selecting courses-贪心-[解题报告]HOJ
2014
11-30

HDU 3697-Selecting courses-贪心-[解题报告]HOJ

Selecting courses

问题描述 :

    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.

输入:

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.

输出:

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.

样例输入:

2
1 10
4 5
0

样例输出:

2

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define inf 0x3fffffff
#define N 1100
struct node {
int l,r;
}f[N];
int ff[N];
double d;
int Min(int aa,int bb) {
return aa>bb?bb:aa;
}
int Max(int aa,int bb) {
return aa>bb?aa:bb;
}
int cmp(const void *a,const void *b)
{
    return (*(struct node *)a).r-(*(struct node *)b).r;
}
int main() {
     int n,i,j;
     int minn,maxx,tot,kk,k,ss,endd;
     while(scanf("%d",&n),n) {
            minn=inf;
            maxx=-1;
        for(i=0;i<n;i++) {
            scanf("%d%d",&f[i].l,&f[i].r);
            minn=Min(f[i].l,minn);
            maxx=Max(f[i].r,maxx);
        }
         qsort(f,n,sizeof(f[0]),cmp);
        endd=0;
    for(i=minn;i<=maxx;i++) {
        tot=0;
        memset(ff,0,sizeof(ff));
        for(j=0;j<n;j++) {
         kk=(f[j].l-i)/5;
         if(kk*5+i!=f[j].l)
            kk=kk*5+i+5;
        else
            kk=f[j].l;
         k=(f[j].r-i)/5;
         if(k*5+i==f[j].r)
            k=f[j].r-5;
         else
            k=5*k+i;
            k=Min(k,kk);
          //  printf("k=%d\n",k);
      for(ss=k;ss<f[j].r;ss+=5) {
         if(ff[ss]==0&&ss>=f[j].l&&ss<f[j].r) {
            tot++;
            ff[ss]=1;
            break;
         }
        }
        }
    // printf("i=%d %d\n",i,tot);
        if(endd<tot)
            endd=tot;
    }
    printf("%d\n",endd);
     }
return 0;}

参考:http://blog.csdn.net/u011483306/article/details/39997843


  1. 换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。
    应该是,不可能小于合并后的第K小值吧

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。