int n, k; cin >> n >> k; vector<int>res1; vector<int>res2; vector<int>record(n + 1, 0);
for (int i = 0; i < n; i++) { cin >> record[i]; while (!max_stack.empty() && record[max_stack.back()] < record[i]) { max_stack.pop_back(); } max_stack.push_back(i);
while (!min_stack.empty() && record[min_stack.back()] > record[i]) { min_stack.pop_back(); } min_stack.push_back(i);
while (max_stack.front() <= i - k) { max_stack.pop_front(); } while (min_stack.front() <= i - k) { min_stack.pop_front(); } res1.push_back(max_stack.front()); res2.push_back(min_stack.front());
} for (int i = k - 1; i < n; i++) { printf("%d ", record[res2[i]]); } cout << endl; for (int i = k - 1; i < n; i++) { printf("%d ", record[res1[i]]); } } /* 个人思想: 首先对于一个一个数维护一个双向队列并保证他升序 那么最大的数必然是升序的头元素(ps:在判断是要确定这个元素是否在范围内)如何维护我们趋向于维护一个可能成为最大值的队列如果出现了一个比队列尾部要大的元素自然尾部元素不可能成为最大元素了,需要弹出。 */