暑假前专题题解---数据结构---G

数据结构队列的简单运用,每个元素放进队列,查询的时候,判断当前队首的元素是否合法(有没有下载完成即可),不合法弹出,知道为空或者有合法元素。

代码如下

#include <bits/stdc++.h>
using namespace std;
struct node{ int t,a,b; };
queue<node> q;
int main(){
    int n,opt,a,b,t;
    cin >> n;
    for (int i=0; i<n; i++) {
        cin >> opt;
        if (opt == 1){
            cin >> t >> a >> b;
            q.push({t,a,b});
        }else if (opt == 2){
            cin >> t;
            while (!q.empty()) {
                node now = q.front();
                if (now.t + now.b <= t) q.pop();
                else {q.pop(); break;}
            }
        }else if (opt == 3){
            cin >> t;
            if (q.empty())cout << -1 << endl;
            else{
                int tag = 0;
                while (!q.empty()) {
                    node now = q.front();
                    if (now.t + now.b <= t) q.pop();
                    else { cout << now.a << endl; tag = 1 ; break;}
                }
                if (!tag) cout << -1 << endl;
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/CCCCTong/article/details/81392285