标签:单调队列 数据结构 栈
1117: Ready to declare
时间限制: 1 Sec 内存限制: 128 MB
提交: 358 解决: 41
[提交][状态][讨论版]
题目描述
Finally, you find the most good-looking girl...
You are going to write a letter to her. But you are not convident to be better than other boys. So you think you need a good chance...
You have known that these days, she will meet N boys at all(N<=100000). The ith boy has a handsome value V[i](0<V[i]<10000). She will meet K(K<=N) boys at the same time, and when the first boy leave, the next boy join them. It is to say that she will meet
first K boys in the list at first, then the first boy leave ,the k+1-th boy join. So she will meet boys N-K+1 times.
You must want to join she and them when these boys are not handsome, so you need to calculate each meet‘s the least handsome value and the most.
here is a sample when N=8,K=3
boys least most
[5 3 2] 1 1 10 2 3 2
5
5 [3 2 1] 1 10 2 3 1
3
5 3 [2 1 1] 10 2 3 1
2
5 3 2 [1 1 10] 2 3 1
10
5 3 2 1 [1 10 2] 3 1
10
5 3 2 1 1 [10 2 3] 2
10
输入
There are several test cases, each case contains two lines.
The first line is two number ,N K.
The second line has N number, the handsome value.
输出
Each case output two lines.The first line is each meet‘s min handsome value, the second line is each meet‘s max handsome value.
样例输入
8 3
5 3 2 1 1 10 2 3
样例输出
2 1 1 1 1 2
5 3 2 10 10 10
提示
来源
虽然正解是单调队列,可是我用的是两个栈实现的队列水过的,存下代码,这种比单调队列支持的更多
/*************************************************************************
> File Name: Euler.cpp
> Author: acvcla
> QQ:
> Mail: acvcla@gmail.com
> Created Time: 2014年10月30日 星期四 11时19分11秒
************************************************************************/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=1e5+5;
int stack1[maxn],stack2[maxn],max2[maxn],max1,min2[maxn],min1;
int top1,top2;
void init(){
top1=top2=0;
max1=max2[top2]=0;
min1=min2[top2]=maxn;
}
int ans1[maxn],ans2[maxn];
void deque(){
if(top2>0){
stack2[--top2];
return ;
}
while(top1>0){
stack2[top2]=stack1[--top1];
max2[top2]=max(top2>0?max2[top2-1]:0,stack2[top2]);
min2[top2]=min(top2>0?min2[top2-1]:maxn,stack2[top2]);
top2++;
}
max1=0;
min1=maxn;
--top2;
}
int get_max_min(int x){
if(x)return max(max1,top2>0?max2[top2-1]:0);
return min(min1,top2>0?min2[top2-1]:maxn);
}
void push(int x){
max1=max(x,max1);
min1=min(x,min1);
stack1[top1++]=x;
}
int main()
{
int n,k,x;
while(~scanf("%d%d",&n,&k)){
init();
int t=0,cnt=0;
for(int i=1;i<=n;i++){
scanf("%d",&x);
push(x);
++cnt;
if(cnt==k)
{
ans1[t]=get_max_min(0);
//cout<<"sldl"<<endl;
ans2[t++]=get_max_min(1);
deque();
cnt--;
}
}
for(int i=0;i<t;i++)printf("%d%c",ans1[i],i==t-1?'\n':' ');
for(int i=0;i<t;i++)printf("%d%c",ans2[i],i==t-1?'\n':' ');
}
return 0;
}
NEUOJ 1117: Ready to declare(单调队列)
标签:单调队列 数据结构 栈
原文地址:http://blog.csdn.net/acvcla/article/details/42056031