听灌佬多
  • 板块灌水区
  • 楼主gjr19509
  • 当前回复41
  • 已保存回复42
  • 发布时间2024/8/21 09:57
  • 上次更新2024/8/21 12:05:33
查看原帖
听灌佬多
1057116
gjr19509楼主2024/8/21 09:57
#include <iostream>
#include <algorithm>
#include <deque>
#include <sstream>
#define int int_fast64_t
using namespace std;
int n,m;
signed main() {
    cin >> n >> m;
    int a[n + 1];
    cin>>a[1];
    deque<int> mindeq,maxdeq;
    mindeq.push_back(1);
    maxdeq.push_front(1);
    ostringstream min,max;
    for (int i = 2; i <= n; i++) {
        cin >> a[i];
        while (!mindeq.empty()&&i-mindeq.front()>m){
            mindeq.pop_front();
        }
        while (!mindeq.empty()&&a[mindeq.back()]>a[i]){
            mindeq.pop_back();
        }
        if (i>3)min<<a[mindeq.front()]<<' ';
        mindeq.push_back(i);
        while (!maxdeq.empty()&&i-maxdeq.front()>m){
            maxdeq.pop_front();
        }
        while (!maxdeq.empty()&&a[maxdeq.back()]<a[i]){
            maxdeq.pop_back();
        }
        if (i>3)max<<a[maxdeq.front()]<<' ';
        maxdeq.push_back(i);
    }
    cout<<min.str()<<*min_element(a+n-3,a+n+1)<<endl<<max.str()<<*max_element(a+n-3,a+n+1);
    return 0;
}

题目 评测记录

2024/8/21 09:57
加载中...