csp认证 201403-2 窗口

加上题目审核不通过,我吐了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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值