HITvici @ 2024-03-09 21:24:48
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[100000]={0};
deque<int> q;
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
while(!q.empty()&&a[q.back()]>a[i])//a[i]相当于是新来的元素
q.pop_back();
q.push_back(i);
if(i>=k)
{
while(!q.empty()&&q.front()<=i-k)
q.pop_front();
cout<<a[q.front()]<<' ';
}
}
cout<<'\n';
while(!q.empty())
q.pop_front();
for(int i=1;i<=n;i++)
{
while(!q.empty()&&a[q.back()]<a[i])//a[i]相当于是新来的元素
q.pop_back();
q.push_back(i);
if(i>=k)
{
while(!q.empty()&&q.front()<=i-k)
q.pop_front();
cout<<a[q.front()]<<' ';
}
}
return 0;
}
by gavinliu266 @ 2024-03-09 21:28:06
a数组和deque开在main()函数外面
by gavinliu266 @ 2024-03-09 21:28:36
建议用scanf与printf
by gavinliu266 @ 2024-03-09 21:29:27
a数组开
by Composite_Function @ 2024-03-09 21:38:20
#include <bits/stdc++.h>
using namespace std;
int a[1000001]={0};
int main()
{
deque<int> q;
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
while(!q.empty()&&a[q.back()]>a[i])//a[i]相当于是新来的元素
q.pop_back();
q.push_back(i);
if(i>=k)
{
while(!q.empty()&&q.front()<=i-k)
q.pop_front();
cout<<a[q.front()]<<' ';
}
}
cout<<'\n';
while(!q.empty())
q.pop_front();
for(int i=1;i<=n;i++)
{
while(!q.empty()&&a[q.back()]<a[i])//a[i]相当于是新来的元素
q.pop_back();
q.push_back(i);
if(i>=k)
{
while(!q.empty()&&q.front()<=i-k)
q.pop_front();
cout<<a[q.front()]<<' ';
}
}
return 0;
}
就好了。
by HITvici @ 2024-03-09 21:47:22
@gavinliu266 解决了,谢谢大佬
by HITvici @ 2024-03-09 21:47:44
@Composite_Function 谢谢大佬,问题解决了