liguanda @ 2024-05-19 16:18:41
#include<bits/stdc++.h>
using namespace std;
int n,m;
int main (){
cin >> n;
cin >> m;
int a[m+2];
for (int i = 1; i <= m; i++ ){
cin >> a[i];
}
int maxx=a[1];
int less=a[1];
for (int i = 1; i <= n; i++ ){
for (int j = i; j <= m; j++ ){
if (a[j] < less){
less=a[j];
}
}
cout << less << " ";
for (int j = i; j <= m; j++ ){
a[i]=a[i+1];
}
cin >> a[i+m];
}
cout << endl;
for (int i = 1; i <= n; i++ ){
for (int j = i; j <= m; j++ ){
if (a[j] > maxx){
maxx=a[j];
}
}
cout << maxx << " ";
for (int j = i; j <= m; j++ ){
a[i]=a[i+1];
}
cin >> a[i+m];
}
return 0;
}
by Louis_lxy @ 2024-05-29 20:16:20
问题过大,建议重写
by Louis_lxy @ 2024-05-29 20:18:04
建议重看题目
by huangshuchang @ 2024-06-08 08:11:53
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1;
int n,k;
struct node{
int v,id;
}a[N];
deque<node> qmax;
deque<node> qmin;
int main(){
//freopen("two.in","r",stdin);
//freopen("two.out","w",stdout);
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i].v,a[i].id=i;
for(int i=1;i<=n;i++){
while(!qmin.empty()&&qmin.back().v>=a[i].v){
qmin.pop_back();
}
qmin.push_back(a[i]);
if(qmin.front().id<=i-k){
qmin.pop_front();
}
if(i>=k){
cout<<qmin.front().v<<" ";
}
}
cout<<endl;
for(int i=1;i<=n;i++){
while(!qmax.empty()&&qmax.back().v<=a[i].v){
qmax.pop_back();
}
qmax.push_back(a[i]);
if(qmax.front().id<=i-k){
qmax.pop_front();
}
if(i>=k){
cout<<qmax.front().v<<" ";
}
}
return 0;
}
@liguanda 你写的……我没法改,这是道模板题
by huangshuchang @ 2024-06-08 08:16:22
@liguanda 建议做做这个
by cgxd @ 2024-06-16 08:25:51
这代码我看晕了,建议重写,建议用2个deque做,一个记录最大值,一个记录最小值,再输出