给定一个区间长度n和一串数字,以-1结尾,输出每段长度为n的连续数中的最大值.
#include <cstdio>
#include <deque>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
int main(int argc, char const *argv[]) {
int n;
scanf("%d", &n);
deque<P> q;
int val, i = 0;
while (scanf("%d", &val) == 1 && val != -1) {
while (!q.empty() && q.back().first <= val) {
q.pop_back();
}
q.push_back(P(val, i));
if (i - n + 1 >= 0) {
while (!q.empty() && q.front().second < i - n + 1) {
q.pop_front();
}
printf("%d\n", q.front().first);
}
i++;
}
return 0;
}