POJ2376 Cleaning Shifts 贪心

    这题贪心法,首先按照每头牛工作开始时间进行升序排序,然后在按顺序选取雇佣的牛,选择的时候要满足:

1,当前查询的牛的开始工作时间小于等于上一次选中的牛的工作结束时间;

2,当前查询的牛工作结束时间最晚。

当依次查询完毕后,就能得到答案。

#ifndef HEAD
#include <stdio.h>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <iostream>
#include <queue>
#include <list>
#include <algorithm>
#include <stack>
#include <map>

using namespace std;
#endif // !HEAD
struct TimeSeg
{
	int start;
	int end;
};

bool CSSortor(TimeSeg& a1, TimeSeg& a2)
{
	return a1.start < a2.start;
}

int main()
{
#ifdef _DEBUG
	freopen("e:\\in.txt", "r", stdin);
#endif
	int n, m;
	
	scanf("%d %d\n", &n, &m);
	vector<TimeSeg> vecTS;
	for (int i = 0; i < n; i++)
	{
		TimeSeg ts;
		scanf("%d %d\n", &ts.start, &ts.end);
		vecTS.push_back(ts);
	}
	sort(vecTS.begin(), vecTS.end(), CSSortor);
	TimeSeg ts;
	ts.start = m + 1;
	ts.end = 0;
	vecTS.push_back(ts);
	int start =  0;
	int maxEnd = 0;
	int curCount = 0;
	for (int i = 0; i < n + 1;i++)
	{
		if (vecTS[i].start <= start  + 1 && vecTS[i].end >= maxEnd)
		{
			maxEnd = vecTS[i].end;
		}
		else if (vecTS[i].start > start + 1)
		{	
			start = maxEnd;
			curCount++;
			if (vecTS[i].start <= start + 1)
			{
				maxEnd = max(maxEnd, vecTS[i].end);
			}
			else
				break;
			
		}
	}
	if (maxEnd >= m)
	{
		printf("%d\n", curCount);
	}
	else
		printf("-1\n");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值