加上题目审核不通过,我吐了QAQ
解
运用list的结构,使用erase和emplace_front 操作完成窗口的位置变化。
//
// Created by 29273 on 2021-04-05.
//
#include "bits/stdc++.h"
using namespace std;
struct menu {
int x1, x2, y1, y2, id;
menu(int x1, int y1, int x2, int y2, int id) : x1(x1), x2(x2), y1(y1), y2(y2), id(id) {
};
};
int container(int a, int b, int x1, int x2, int y1, int y2) {
return a <= x2 && a >= x1 && b >= y1 && b <= y2;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
list<menu> v;
int n, m, a, b, c, d, e, f;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a >> b >> c >> d;
v.push_front(menu(a, b, c, d, i));
}
// for (auto it: v)
// printf("%d %d %d %d %d\n", it.x1, it.y1, it.x2,it.y2, it.id);
while (m--) {
cin >> e >> f;
auto it = v.begin();
// 可使用上面的container函数判断
while (it != v.end() && !(e >= it->x1 && e <= it->x2 && f >= it->y1 && f <= it->y2)) {
// printf("%d %d\n", it != v.end(), !container(e, f, it->x1, it->x2, it->y1, it->y2));
it++;
}
if (it == v.end()) {
printf("IGNORED\n");
} else {
printf("%d\n", it->id);
v.erase(it);
v.push_front(*it);
}
}
return 0;
};
错解
使用两个桟来完成窗口位置的更替,但是提交会出错只有二十分,有懂的大佬可以批评指正!
//
// Created by 29273 on 2021-04-05.
//
#include "bits/stdc++.h"
using namespace std;
struct menu {
int x1, x2, y1, y2, id;
menu(int x1, int y1, int x2, int y2, int id) : x1(x1), x2(x2), y1(y1), y2(y2), id(id) {
};
};
int container(int a, int b, int x1, int x2, int y1, int y2) {
return a <= x2 && a >= x1 && b >= y1 && b <= y2;
}
int main() {
stack<menu> Q;
stack<menu> S;
int n, m, a, b, c, d, e, f;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a >> b >> c >> d;
Q.push(menu(a, b, c, d, i));
}
for (int i = 0; i < m; i++) {
cin >> e >> f;
while (!Q.empty()) {
menu front = Q.top();
if (container(e, f, front.x1, front.x2, front.y1, front.y2)) {
printf("%d\n", front.id);
Q.pop();
while (!S.empty()) {
menu top = S.top();
Q.emplace(top);
S.pop();
}
Q.emplace(front);
break;
} else {
S.emplace(menu(front.x1, front.x2, front.y1, front.y2, front.id));
Q.pop();
}
}
if (Q.empty()) {
printf("IGNORED\n");
}
while (!S.empty()) {
menu top = S.top();
Q.emplace(top);
S.pop();
}
}
return 0;
}