CCF历年题解

CCF201709-2 公共钥匙盒(100分)

排序一下就可以了,用结构体来存储,结构体中包含钥匙编号,借还标志,借还时间,

排序按照时间为第一关键字,

还的优先级高于借的

都是还的话就按钥匙编号小的优先

借的就无所谓了

写的时候不仔细,最后一个循环少加了break,卡了好久

#include
#include
#include
#include
#pragma warning(disable:4996)
using namespace std;
const int Max = 2000 + 5;
struct Node
{
	int t, flag, id;
	bool operator<(const Node &b)const {
		if (t == b.t) {
			if (flag == b.flag) return id < b.id;
			return flag < b.flag;
		}
		return t < b.t;
	}
};
Node teh[Max];

int main()
{
	int N, k, id, s, t;
	scanf("%d%d", &N, &k);
	for (int i = 1; i <= k; i++) {
		scanf("%d%d%d", &id, &s, &t);
		teh[i].id = teh[i + k].id = id;
		teh[i].flag = 1; teh[i + k].flag = -1;
		teh[i].t = s; teh[i + k].t = s + t;
	}
	sort(teh + 1, teh + 2 * k + 1);
	int vis[Max];
	for (int i = 1; i <= N; i++) vis[i] = i;
	for (int i = 1; i <= 2 * k; i++)
	{
		if (teh[i].flag == 1) {
			for (int j = 1; j <= N; j++) if (vis[j] == teh[i].id) {
				vis[j] = -1; break;
			}
		}
		else {
			for (int j = 1; j <= N; j++) if (vis[j] == -1) {
				vis[j] = teh[i].id; break;
			}
		}
	}
	printf("%d", vis[1]);
	for (int i = 2; i <= N; i++) printf(" %d", vis[i]);
	puts("");
	system("pause");
	return 0;
}

 

你可能感兴趣的:(CCF题目)