SDUT 活动选择

活动选择

Description
学校的大学生艺术中心周日将面向全校各个学院的学生社团开放,但活动中心同时只能供一个社团活动使用,并且每一个社团活动开始后都不能中断。现在各个社团都提交了他们使用该中心的活动计划(即活动的开始时刻和截止时刻)。请设计一个算法来找到一个最佳的分配序列,以能够在大学生艺术中心安排不冲突的尽可能多的社团活动。
比如有5个活动,开始与截止时刻分别为:

最佳安排序列为:1,4,5。

Input
第一行输入活动数目n(0<n<100);
以后输入n行,分别输入序号为1到n的活动使用中心的开始时刻a与截止时刻b(a,b为整数且0<=a,b<24,a,b输入以空格分隔)。

Output
输出最佳安排序列所包含的各个活动(按照活动被安排的次序,两个活动之间用逗号分隔),如果有多个活动安排序列符合要求输出字典序最小的序列。

Samples
Sample #1
Input
Output
6
8 10
9 16
11 16
14 15
10 14
7 11
1,5,4

分析:

结构体方法:
sort函数不稳定
stable_sort函数稳定

#include <bits/stdc++.h>
using namespace std;
const int N=3000010;
struct node{
	int id;
	int begin;
	int end;
}a[110],t;
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i].begin>>a[i].end;
		a[i].id=i+1;
	}
	for(int i=0;i<n-1;i++){
		for(int j=0;j<n-1-i;j++){
			if(a[j].end>a[j+1].end){
				t=a[j];
				a[j]=a[j+1];
				a[j+1]=t;
			}
		}
	}
	int k=a[0].end;
	cout<<a[0].id;
	for(int i=1;i<n;i++){
		if(k<=a[i].begin){
			cout<<","<<a[i].id;
			k=a[i].end;
		}
	}
	cout<<endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N=3000010;
struct node{
	int id,begin,end;
}a[110],t;
int cmp(struct node a,struct node b){
	return a.end<b.end;
}
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i].begin>>a[i].end;
		a[i].id=i+1;
	}
	//sort不稳定
	stable_sort(a,a+n,cmp);
	int k=a[0].end;
	cout<<a[0].id;
	for(int i=1;i<n;i++){
		if(k<=a[i].begin){
			cout<<","<<a[i].id;
			k=a[i].end;
		}
	}
	cout<<endl;
	return 0;
}
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值