蒟蒻求助,单调队列T了两个点:#2 #10

P1440 求m区间内的最小值

XLost @ 2019-08-26 14:28:07

#include <bits/stdc++.h>

struct Info {
    int time, var;
};

int n, m, num;
std::deque <Info> Q;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin >> n >> m;
    for (register int i = 1; i <= n; i++) {
        std::cin >> num;
        if (Q.empty()) {
            std::cout << 0 << std::endl;
        } else {
            if (Q.front().time + m < i) {
                Q.pop_front();
            }
            std::cout << Q.front().var << std::endl;
        }
        while (!Q.empty() && Q.back().var >= num) {
            Q.pop_back();
        }
        Info im = {i, num};
        Q.push_back(im);
    }
    return 0;
}

救救孩子……


by Smile_Cindy @ 2019-08-26 14:53:06

@Lost_person

帮你改过了

#include <bits/stdc++.h>
#define endl '\n'

struct Info {
    int time, var;
};

int n, m, num;
std::deque <Info> Q;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0); 
    std::cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        std::cin >> num;
        if (Q.empty()) {
            std::cout << 0 << endl;
        } else {
            if (Q.front().time + m < i) {
                Q.pop_front();
            }
            std::cout << Q.front().var << endl;
        }
        while (!Q.empty() && Q.back().var >= num) {
            Q.pop_back();
        }
        Info im = {i, num};
        Q.push_back(im);
    }
    return 0;
}

by XLost @ 2019-08-26 14:58:19

@Alpha 好的好的


by XLost @ 2019-08-26 14:58:40

多谢大佬


上一页 |