题目:
会场安排问题
时间限制:
3000 ms | 内存限制:
65535 KB
难度:
4
-
描述
-
学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办。小刘的工作就是安排学校小礼堂的活动,每个时间最多安排一个活动。现在小刘有一些活动计划的时间表,他想尽可能的安排更多的活动,请问他该如何安排。
因为我们要尽可能的选择结束时间较早的,所以我们对b排序,然后从前向后扫描,计数即可。。具体的讲解参考小白153页!!!
代码为:
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=10000+10;
struct time
{
int st,en;
}a[maxn];
bool cmp(struct time a,struct time b)
{
if(a.en!=b.en)
return a.en<b.en;
else
return a.st<b.st;
}
int main()
{
int i,m,n,pos,count;
count=1;
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d%d",&a[i].st,&a[i].en);
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++)
printf("%d %d \n",a[i].st,a[i].en);
pos=a[1].en;
for(i=2;i<=n;i++)
{
if(a[i].st>pos)
{
pos=a[i].en;
count++;
}
}
printf("%d\n",count);
}
return 0;
}
/*
4
7
1 10
11 15
11 12
13 15
16 24
17 24
25 40
*/