POJ 3988 Selecting Courses (Greedy)
{POJ} {3988} {Selecting courses}
Greedy Algorithm, time is fixed and just find the earlest finished courses.
#include <iostream>
#include <string>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;
#define rep(a,x,y) for(a=x;a<y;++a)
#define CLR(x,y) memset(x,y,sizeof(x))
int n,ans;
typedef struct{
int a,b;
}Node;
bool operator<(const Node& a, const Node& b)
{
if(a.b == b.b)
return a.a < b.a;
return a.b < b.b;
}
int work()
{
int i,j,tmp;
int a,b,k;
bool v[500];
Node node[500];
while(scanf("%d",&n)) {
if( 0 == n )
break;
rep(i,0,n)
scanf("%d%d",&node[i].a,&node[i].b);
sort(node,node+n);
ans = 0;
rep(i,0,5){
rec = 0;
tmp = 0;
CLR(v,0);
for( j = i; j < 1005; j += 5){
rep(k,0,n) {
if( !v[k] && node[k].a <= j && node[k].b > j ){
v[k] = true;
++tmp;
break;
}
}
}
if( ans < tmp )
ans = tmp;
}
printf("%d\n",ans);
}
return 0;
}
int main()
{
work();
return 0;
}
浙公网安备 33010602011771号