会场安排(贪心)

会场安排问题

Time Limit: 1 Sec   Memory Limit: 64 MB
Submit: 1670   Solved: 375
[ Submit][ Status][ Discuss]

Description

假设要在足够多的会场里安排一批活动,并希望使用尽可能少的会场。设计一个有效的贪心算法进行安排。(这个问题实际上是著名的图着色问题。若将每一个活动作为图的一个顶点,不相容活动间用边相连。使相邻顶点着有不同颜色的最小着色数,相应于要找的最小会场数。)对于给定的k个待安排的活动,计算使用最少会场的时间表。

Input

输入数据的第一行有1 个正整数k(k≤10000),表示有k个待安排的活动。接下来的k行中,每行有2个正整数,分别表示k个待安排的活动开始时间和结束时间。时间以0 点开始的分钟计。

Output

输出一个整数,表示最少会场数。

Sample Input

5
1 23
12 28
25 35
27 80
36 50

Sample Output

3

HINT

Source

[ Submit][ Status][ Discuss]



贪心问题要求,所用会场最少。

可以按照会议的开始时间或者结束时间排序(可能是我比较水,用结束时间排序一直没能A。你们可以自己试一下)。

然后遍历一边,看一下那几场会议时间不冲突(区间不相交),给他们安排同一个会场。

最后输出最少的会场数

AC代码:
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <deque>
#include <stack>
using namespace std ;
struct A {
int a ;
int b ;
int c ;
} a [ 11000 ];
bool cmp ( struct A a , struct A b )
{
   
return a . a < b . a ;
}
int main ()
{
   
int i , j , k , n , m , x ;
   
while ( scanf ( "%d" ,& n )!= EOF )
   
{
        x
= 1 ;
       
for ( i = 0 ; i < n ; i ++)
       
{
           
scanf ( "%d%d" ,& a [ i ]. a ,& a [ i ]. b ); a [ i ]. c = 0 ;
       
}
       
sort ( a , a + n , cmp );
       
for ( i = 0 ; i < n ; i ++)
       
{
            m
= a [ i ]. b ;
           
if ( a [ i ]. c == 0 )
           
{
            a
[ i ]. c = x ;
           
for ( j = i +1 ; j < n ; j ++)
           
{
               
if ( a [ j ]. c == 0 )
               
{
                 
if ( m <= a [ j ]. a )
                a
[ j ]. c = x ,
                m
= a [ j ]. b ;


               
}
           
} x ++;




           
}
       
}
       
printf ( "%d \n " , x -1 );
   
}

}

 
  

猜你喜欢

转载自blog.csdn.net/qq_41232172/article/details/79836275