活动选择
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Problem Description
学校的大学生艺术中心周日将面向全校各个学院的学生社团开放,但活动中心同时只能供一个社团活动使用,并且每一个社团活动开始后都不能中断。现在各个社团都提交了他们使用该中心的活动计划(即活动的开始时刻和截止时刻)。请设计一个算法来找到一个最佳的分配序列,以能够在大学生艺术中心安排不冲突的尽可能多的社团活动。
比如有5个活动,开始与截止时刻分别为:
最佳安排序列为:1,4,5。
比如有5个活动,开始与截止时刻分别为:
![](https://i-blog.csdnimg.cn/blog_migrate/87a4ef7ef99da5b1ee4f150d5ca89f56.png)
最佳安排序列为:1,4,5。
Input
第一行输入活动数目n(0<n<100);
以后输入n行,分别输入序号为1到n的活动使用中心的开始时刻a与截止时刻b(a,b为整数且0<=a,b<24,a,b输入以空格分隔)。
以后输入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 <stdio.h>
#include <string.h>
struct node
{
int e, b, num;
};
int main ()
{
struct node ac[100], t;
int i, j, n;
scanf("%d", &n);
for(i=0; i<n; i++)
{
scanf("%d %d", &ac[i].b, &ac[i].e);
ac[i].num = i + 1;
}
for(i=0; i<n-1; i++)
{
for(j=0; j<n-1-i; j++)
{
if(ac[j].e > ac[j+1].e)
{
t = ac[j];
ac[j] = ac[j+1];
ac[j+1] = t;
}
}
}
int st = ac[0].e;
printf("%d", ac[0].num);
for(i=1; i<n; i++)
{
if(st <= ac[i].b)
{
printf(",%d", ac[i].num);
st = ac[i].e;
}
}
printf("\n");
return 0;
}