pip202513 @ 2023-07-16 15:58:55
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int n,k,a[1000005],dp[1000005];
void dp_min()
{
deque<int> q,t;
for(int i=1;i<=n;i++)
{
while(q.front()<i-k+1&&!q.empty()) q.pop_front(),t.pop_front();
while(a[i]<=t.back()&&!t.empty()) q.pop_back(),t.pop_back();
q.push_back(i); t.push_back(a[i]);
dp[i]=t.front();
cout<<dp[i]<<" ";
}
return;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
dp_min(); //dp_max();
for(int i=k;i<=n;i++) cout<<dp[i]<<" ";
return 0;
}
by dry_ @ 2023-07-18 21:10:15
!q.empty()和!t.empty()放在前面
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int n,k,a[1000005],dp[1000005];
void dp_min()
{
deque<int> q,t;
for(int i=1;i<=n;i++)
{
while(!q.empty() && q.front()<i-k+1) q.pop_front(),t.pop_front();
while(!t.empty() && a[i]<=t.back()) q.pop_back(),t.pop_back();
q.push_back(i); t.push_back(a[i]);
dp[i]=t.front();
// cout<<dp[i]<<" ";
}
return;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
dp_min(); //dp_max();
for(int i=k;i<=n;i++) cout<<dp[i]<<" ";
return 0;
}
by aaa_lvzekai @ 2023-08-04 21:59:12
@pip202513