引言:ZZU的朋友们请不要直接抄,我只是记录每次做题的成果,别查重完之后大家一起GG。
会场安排问题
题目来源:王晓东《算法设计与分析》
假设要在足够多的会场里安排一批活动,并希望使用尽可能少的会场。设计一个有效的 贪心算法进行安排。(这个问题实际上是著名的图着色问题。若将每一个活动作为图的一个 顶点,不相容活动间用边相连。使相邻顶点着有不同颜色的最小着色数,相应于要找的最小 会场数。)
输入格式:
第一行有 1 个正整数k,表示有 k个待安排的活动。 接下来的 k行中,每行有 2个正整数,分别表示 k个待安排的活动开始时间和结束时间。时间 以 0 点开始的分钟计。
输出格式:
输出最少会场数。
输入样例:
5
1 23
12 28
25 35
27 80
36 50
输出样例:
在这里给出相应的输出。例如:
3
Code:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+7;
using namespace std;
#define ARR_SIZE 10001
#define Num 500005
int main(){
int time;
int k;
cin>>k;
int begin[k],end[k];
int AllB[k],AllE[k];
for(int i = 0; i < k; i++){
cin>>begin[i]>>end[i];
AllB[i] = AllE[i] = -1;
}
AllB[0] = begin[0];
AllE[0] = end[0];
int res = 1;
for(int i = 0; i < k-1; i++){
for(int j = k-1; j >= i; j--){
if(begin[j] < begin[j-1]){
int m = begin[j];
begin[j] = begin[j-1];
begin[j-1] = m;
m = end[j];
end[j] = end[j-1];
end[j-1] = m;
}
}
}
for(int i = 1; i < k; i++){
for(int j = 0; j < res; j++){
if( begin[i] < AllE[j]){
if(j == res-1){
AllB[res] = begin[i];
AllE[res] = end[i];
res++;
break;
}
else continue;
}
else{
AllE[j] = end[i];
break;
}
}
}
cout<<res;
}