SDUT_活动选择问题2_贪心

活动选择问题

Time Limit: 1000 ms  Memory Limit: 65536 KiB
Problem Description
 sdut 大学生艺术中心每天都有n个活动申请举办,但是为了举办更多的活动,必须要放弃一些活动,求出每天最多能举办多少活动。
Input
 输入包括多组输入,每组输入第一行为申请的活动数n(n<100),从第2行到n+1行,每行两个数,是每个活动的开始时间b,结束时间e;
Output
 输出每天最多能举办的活动数。
Sample Input
12
15 20
15 19
8 18
10 15
4 14
6 12
5 10
2 9
3 8
0 7
3 4
1 3
Sample Output
5
Hint
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct activity{
	int tag;
	int begin;
	int end;
}activity;
struct activity act[100];
int res[100];
bool cmp(struct activity x,struct activity y){
	return x.end<y.end;
}
int main(){
	int n;
	
	while(~scanf("%d",&n)){
	
	for(int i=0;i<n;i++){
		scanf("%d%d",&act[i].begin,&act[i].end);
		act[i].tag=i+1;
	}
	sort(act,act+n,cmp);
	bool a[100];
	a[0]=true;
	int j=0;
	for(int i=1;i<n;i++){
		if(act[i].begin>=act[j].end){     //贪心算法 
			a[i]=true;
			j=i;
		}
		else{
			a[i]=false;
		}
	}
	int p=0;
	for(int i=0;i<n;i++){
		if(a[i]){
			res[p++]=act[i].tag;
		}
	}
	printf("%d\n",p);
	
    }
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值