nyoj 1036 非洲小孩

本题与nyoj 891 找点问题 类似, 都属于贪心算法。

nyoj 891的链接:nyoj 891 找点

解题思路:如果输入的开始时间大于输入结束时间,交换两时间, 然后以结束时间从小到大的顺序进行排序,再以开始时间从小到大的顺序进行排序, 如果后一个的开始时间大于前一个的结束时间,则进行统计, 最后输出统计结果

AC代码:

# include <stdio.h>
# include <algorithm>

struct TIME
{
	int h1;
	int m1;
	int h2;
	int m2;
};

int cmp(const void * c, const void * d)
{
	TIME * a = (TIME *)c;
	TIME * b = (TIME *)d;
	if (a->h2 != b->h2)
	{
		return a->h2 - b->h2;
	}
	else if (a->m2 != b->m2)
	{
		return a->m2 - b->m2;
	}
	else if (a->h1 != b->h1)
	{
		return a->h1 - b->h1;		
	}	
	else if (a->m1 != b->m1)
	{		
		return a->m1 - b->m1;		
	}	
}

int main(void)
{	
	TIME t[105];	
	int n;	
	while (scanf("%d", &n) != EOF)		
	{
		int i;		
		for (i = 0; i < n; i++)
		{			
			scanf("%d:%d-%d:%d", &t[i].h1, &t[i].m1, &t[i].h2, &t[i].m2);			
			if (t[i].h2 < t[i].h1)				
			{
				int t1 = t[i].h2;	
				t[i].h2 = t[i].h1;				
				t[i].h1 = t1;				
				int t2 = t[i].m2;				
				t[i].m2 = t[i].m1;				
				t[i].m1 = t2;				
			}			
			else if (t[i].h2 == t[i].h1 && t[i].m2 < t[i].m1)				
			{				
				int t3 = t[i].m1;				
				t[i].m1 = t[i].m2;
				t[i].m2 = t3;				
			}			
		}		
		qsort(t, n, sizeof(t[0]), cmp);		
		int th2 = t[0].h2;		
		int tm2 = t[0].m2;		
		int count = 1;		
		for (i = 1; i < n; i++)			
		{			
			if (t[i].h1 > th2)				
			{				
				count ++;
				th2 = t[i].h2;			
				tm2 = t[i].m2;
			}			
			else if (t[i].h1 == th2 && t[i].m1 > tm2)				
			{				
				count ++;	
				th2 = t[i].h2;			
				tm2 = t[i].m2;
			}			
		}		
		printf("%d\n", count);		
	}	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值