算法第六节(第4部分: 求宣讲会的最多场次)

#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
using namespace std;
//
//  test.h
//  test
//
//  Created by 吴珝君 on 2018/12/31.
//  Copyright  ©   2018年 闲着也是贤者. All rights reserved.
//
/************************************************************************/
/************************************************************************/
/* 
一些项目要占用一个会议室宣讲,会议室不能同时容纳两个项目 的宣讲。 
给你每一个项目开始的时间和结束的时间(给你一个数 组,里面 是一个个具体的项目),
你来安排宣讲的日程,要求会 议室进行 的宣讲的场次最多。返回这个最多的宣讲场次
*/
/************************************************************************/
/************************************************************************/
/* 算法思想:
谁先结束,我们就让谁先宣讲。宣讲结束后,再从最先结束宣讲并且还没有错过开始时间的
项目进行宣讲
*/
/************************************************************************/
class project
{

public:
	project(int s, int e)
	{
		start = s;
		end = e;

	}
	bool operator>(project p)//小顶堆
	{
		return this->end > p.end;//

	}
	int start;//宣讲开始时间和结束时间
	int end;
};
class com
{
public:
	bool operator()(project p1, project p2)
	{

		return p1.end > p2.end;//从大到小排列 小顶堆
	}
};
class  Midian 
{
public:
	int getMaxCount(vector<project> v)
	{
		for(int i = 0; i < v.size(); i++)
		{
			pMin.push(v[i]);	
		}

		//
		int res = 0;
		int t = 0;

		while(!pMin.empty())
		{

			if (t < pMin.top().start)
			{
				res++;
				t = pMin.top().end;
			}
			pMin.pop();

		}
		return res;

	}
private:
	priority_queue<project, vector<project>, com> pMin;//大顶堆
}; 
//优先级队列

int main()
{
	project p1(1,1);
	project p2(2,2);
	project p3(2,3);
	project p4(4,2);
	project p5(6,2);
	vector<project> v;
	v.push_back(p1);
	v.push_back(p2);	
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	Midian m;
	cout << m.getMaxCount(v);
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_39804483/article/details/87827409