Codeforces 799B - T-shirt buying

cf 一般都在晚上,这一场没做,听同学说这个题很迷,来了兴趣,就做了一下

开始想用set ,但感觉好恶心的,不易实现,细思之下想到了好的办法,算是 “贪心” 了一下

运用 queue 先进先出 结合 结构体,嗯,不错

这种顺序问题,最好寻找一种规律,想到了 会很简单;整个代码我也就敲了20分钟吧,前面思考的过程耗时间

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<set>
#include<queue>
using namespace std;
const int maxn = 200000 + 50;
int n, m;
queue<int> q1;
queue<int> q2;
queue<int> q3;

struct node {
    int pr;
    int f;
    int x;
    int y;
};
node a[maxn];

bool cmp(node a, node b) {
    return a.pr < b.pr;
}
void clear_() {
    while(!q1.empty()) q1.pop();
    while(!q2.empty()) q2.pop();
    while(!q3.empty()) q3.pop();
}

void init() {
    clear_();
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        scanf("%d", &a[i].pr);
        a[i].f = 1;
    }
    for(int i = 0; i < n; ++i) scanf("%d", &a[i].x);
    for(int i = 0; i < n; ++i) scanf("%d", &a[i].y);
    sort(a, a+n, cmp);
    for(int i = 0; i < n; ++i) {
        if(a[i].x == 1 || a[i].y == 1) q1.push(i);
        if(a[i].x == 2 || a[i].y == 2) q2.push(i);
        if(a[i].x == 3 || a[i].y == 3) q3.push(i);
    }
}

void solve(){
    scanf("%d", &m);
    for(int i  = 0; i < m; ++i) {
        int ff = 0, b;
        scanf("%d", &b);
    if(i) cout << " ";
        if(b == 1) {
            while(!q1.empty()) {
                int t = q1.front();
                if(a[t].f) {
                    a[t].f = 0;
                    cout << a[t].pr;
                    ff = 1;
                }
                q1.pop();
                if(ff) break;
            }
            if(!ff) cout << -1;
        }

        else if(b == 2) {
            while(!q2.empty()) {
                int t = q2.front();
                if(a[t].f) {
                    a[t].f = 0;
                    cout << a[t].pr;
                    ff = 1;
                   //cout << "==" <<
                }
                q2.pop();
                if(ff) break;
            }
            if(!ff) cout << -1;
        }

        else if(b == 3) {
            while(!q3.empty()) {
                int t = q3.front();
                if(a[t].f) {
                    a[t].f = 0;
                    cout << a[t].pr;
                    ff = 1;
                }
                q3.pop();
                if(ff) break;
            }
            if(!ff) cout << -1;
        }
    }
    //cout << endl;
}


int main() {
    init();
    solve();
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值