201403-2 窗口

29 篇文章 0 订阅
22 篇文章 1 订阅

没有考虑到剩余的窗口的层次顺序不变,得分90

#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;

const int N = 10;
deque<int> pr; // Possible region

struct Window {
	int x1, y1, x2, y2;
	bool top;  // top-floor
} w[N + 10];

void process(int x, int y, int n) {
	for (int i = 0; i < n; ++i) {
		// 如果该点在i窗口内
		if (x >= w[i].x1 && x <= w[i].x2 && y >= w[i].y1 && y <= w[i].y2) {
			pr.push_back(i); // 记录该点可能在的窗口下标
		}
	}
	int L = pr.size();
	if (L > 0) {
		int top_index = 0;
		for (int i = 0; i < n; ++i) {
			if (w[i].top) {
				top_index = i;
			}
		}
		if (find(pr.begin(), pr.end(), top_index) != pr.end()) {
			cout << top_index + 1 << endl;
		} else {
			cout << pr[L - 1] + 1 << endl;
			w[top_index].top = false;
			w[pr[L - 1]].top = true;
		}
	} else {
		cout << "IGNORED" << endl;
	}

	pr.clear();
}

int main() {
	int n, m;
	cin >> n >> m;

	for (int i = 0; i < n; ++i) {
		cin >> w[i].x1 >> w[i].y1 >> w[i].x2 >> w[i].y2;
		w[i].top = false;
	}
	w[n - 1].top = true;
	int x, y;
	while (m--) {
		cin >> x >> y;
		process(x, y, n);
	}

	return 0;
}

优化版,利用一个优先级map存储每个窗口的访问优先级

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

const int N = 10;
map<int, int> prio;  // priority

struct Window {
	int x1, y1, x2, y2;
} w[N + 10];

void process(int x, int y, int n) {
	for (int i = 1; i <= n; ++i) {
		// 从最高层的窗口判断,prio[i]存储优先级为i的窗口序号
		if (x >= w[prio[i]].x1 && x <= w[prio[i]].x2 && y >= w[prio[i]].y1 && y <= w[prio[i]].y2) {
			// 输出被点击窗口的序号
			cout << prio[i] << endl;

			// 更新窗口优先级:序号为1-->n的窗口被访问的顺序为:n-->1
			int tmp = prio[i];
			for (int j = i; j > 1; --j) {
				// 优先级为i的窗口的前面所有窗口,优先级降一
				prio[j] = prio[j - 1];
			}
			prio[1] = tmp;  // 将满足条件的窗口赋予最高的优先级
			return;
		}
	}
	cout << "IGNORED" << endl;
}

int main() {
	int n, m;
	cin >> n >> m;

	for (int i = 1; i <= n; ++i) {
		cin >> w[i].x1 >> w[i].y1 >> w[i].x2 >> w[i].y2;
		prio[i] = n - i + 1;
	}
	int x, y;
	while (m--) {
		cin >> x >> y;
		process(x, y, n);
	}

	return 0;
}

参考链接1:https://blog.csdn.net/tigerisland45/article/details/54782238

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值