刷题:会议室问题

问题是从0-24小时,有人可以定会议室,但是会议室最后分配的准则是1.时间越长的越高优先级  2.之后是按照时间的先后排序。

比如8-10点和9-13点的,会由9-13的点的申请成功。

输入0,0的时候截止输入。

输入示例:

8,10
9,11
13,15
0,0

输出示例:

8,10
13,15

 

这个问题比较简单,但是有几个需要注意一下:

1. 输入的用逗号隔开...很烦;

2. 最后需要升序输出,所以不能最后还需要做一下排序;

思路是申请一个vector长度为24,全为0的vector。每次把最长的段放进去,把0变成1,下一段进来的时候检查是不是全是0,如果全是0,那么把这一段加进去。最后吧数据排序输出。

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


using namespace std;

int findMax(vector<int> vec) {
	int max = -999;
	for (auto v : vec) {
		if (max < v) max = v;
	}
	return max;
}

int getPositionOfMax(vector<int> vec, int max) {
	auto distance = find(vec.begin(), vec.end(), max);
	return distance - vec.begin();
}


int main() 
{
	vector<int> vec1;
	vector<int> vec2;
	vector<int> vec3;
	vector<int> time(24,0);

	while (1)
	{
		int t1, t2;
		char c;

		cin >> t1;
		cin >> c;
		cin >> t2;
		if (t1 == 0 && t2 == 0)
			break;
		vec1.push_back(t1);
		vec2.push_back(t2);
		vec3.push_back(t2-t1);

	}
	vector<int> out1;

	while (vec3.size())
	{
		int maxNumber = findMax(vec3);
		int position = getPositionOfMax(vec3, maxNumber);
		int m = vec1[position];
		int n = vec2[position];
		int count = 0;
		for (int x = m; x < n; x++)
			count = count + time[x - 1];
		if (count == 0)
		{
			for (int x = m; x < n; x++)
				time[x - 1] = 1;
			out1.push_back(m);
			out1.push_back(n);
			vec1.erase(vec1.begin() + position );
			vec2.erase(vec2.begin() + position );
			vec3.erase(vec3.begin() + position );
		}
		else
		{
			vec1.erase(vec1.begin() + position );
			vec2.erase(vec2.begin() + position );
			vec3.erase(vec3.begin() + position );
		}
	}
	std::sort(out1.begin(), out1.end());
	for (auto it = out1.begin(); it != out1.end(); it++)
	{
		cout << *it << ',';
		it++;
		cout << *it << endl;
	}
	return 0;
}

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值