Problem Description
學校的大學生藝術中心週日將面向全校各個學院的學生社團開放,但活動中心同時只能供一個社團活動使用,並且每一個社團活動開始後都不能中斷。現在各個社團都提交了他們使用該中心的活動計劃(即活動的開始時刻和截止時刻)。請設計一個算法來找到一個最佳的分配序列,以能夠在大學生藝術中心安排不衝突的儘可能多的社團活動。
比如有5個活動,開始與截止時刻分別爲:
最佳安排序列爲:1,4,5。
Input
第一行輸入活動數目n(0<n<100);
以後輸入n行,分別輸入序號爲1到n的活動使用中心的開始時刻a與截止時刻b(a,b爲整數且0<=a,b<24,a,b輸入以空格分隔)。
Output
輸出最佳安排序列所包含的各個活動(按照活動被安排的次序,兩個活動之間用逗號分隔)。
Sample Input
6 8 10 9 16 11 16 14 15 10 14 7 11
Sample Output
1,5,4
要選最多的要看結束時間,先用結束時間排序,如果結束時間相同選開始晚的。
當一個活動開始時間大於前一個活動的結束時間時就可以被選取。
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
struct node
{
int s;
int e;
int n;
int h;
}a[105];
bool cmp(node a, node b)
{
if(a.e == b.e)
return a.h < b.h;
return a.e < b.e;
}
int main()
{
int m, i;
cin>>m;
for(i = 1; i <= m; i++)
{
cin>>a[i].s>>a[i].e;
a[i].n = i;
a[i].h = a[i].e - a[i].s;
}
sort(a+1, a+m+1, cmp);
printf("%d", a[1].n);
int x = a[1].e;
for(i = 2; i <= m; i++)
{
if(a[i].s >= x)
{
printf(",%d", a[i].n);
x = a[i].e;
}
}
printf("\n");
return 0;
}