这个题 还是用优先队列吧~~~~
#include<cstdio>
using namespace std;
int tot,win;
int a[1000006],q[1000006];
void getmin(){
int tail=0,head=1;
for(int i=1;i<=tot;i++){
while(head<=tail&&a[q[tail]]>=a[i]) tail--;
q[++tail]=i;
if(i>=win){
while(q[head]<i-win+1) head++;//out of time?
printf("%d ",a[q[head]]);
}
}
printf("\n");
}
void getmax(){
int tail=0,head=1;
for(int i=1;i<=tot;i++){
while(head<=tail&&a[q[tail]]<=a[i]) tail--;
q[++tail]=i;
if(i>=win){
while(q[head]<i-win+1) head++;//out of time?
printf("%d ",a[q[head]]);
}
}
printf("\n");
}
int main(){
scanf("%d%d",&tot,&win);
for(int i=1;i<=tot;i++) scanf("%d",&a[i]);
getmin();getmax();
return 0;
}
需要注意的是 这里的a是真实的数字 而q则是在这里检查是否当玻璃发生移动的时候 返回出需要值的位置 这里的取大和取小实质上都是一种操作,是通过q数组来调节位置的 so....你肯定懂了~~~
最后 要是有大神想出更好的解决方法 请告诉我 跪求打脸QAQ