201709-2公共钥匙盒


#include<iostream>
#include<queue>
using namespace std;

struct _node {
	int num;//钥匙编号
	char op;//操作 g:取钥匙 r还钥匙
	int time;//某时间
	bool operator <(_node a) const
	{
		if (time != a.time)
			return time > a.time;
		else
		{
			if (op != a.op)
				return op < a.op;
			else
			{
				return num > a.num;
			}
		}
	}

};

priority_queue <_node> p;

int hook[1001];


int main()
{
	int n, k;
	cin >> n >> k;
	int w, s, c;
	_node t;

	
	for (int i = 1; i <= n; i++)
	{
		hook[i] = i;
	}


	for (int i = 1; i <= k; i++)
	{
		cin >> w >> s >> c;
		t.num = w;
		t.op = 'g';
		t.time = s;
		p.push(t);


		t.time =s + c;
		t.op = 'r';
		p.push(t);
	}
	
	while (!p.empty())
	{
		t = p.top();
		p.pop();

		if (t.op == 'g')
		{
			for (int i = 1; i <= n; i++)
			{
				if (t.num == hook[i])
				{
					hook[i] = 0;
					break;
				}
			}
		}
		else
		{
			for(int i=1;i<=n;i++)
				if (hook[i] == 0)
				{
					hook[i] = t.num; 
					break;
				}
		}

	}

	for (int i = 1; i <= n; i++)
	{
		cout << hook[i];
		if (i != n)
			cout << " ";
	}
	return 0;
}

抄的,真难,别人做的真简单,多看看

posted @ 2019-08-20 23:19  归根复命  阅读(163)  评论(0编辑  收藏  举报