没有考虑到剩余的窗口的层次顺序不变,得分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