记录编号 |
165873 |
评测结果 |
AAAAAAAAA |
题目名称 |
[POJ 2823]滑动窗口 |
最终得分 |
100 |
用户昵称 |
lenibomb |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.721 s |
提交时间 |
2015-06-13 10:22:57 |
内存使用 |
15.57 MiB |
显示代码纯文本
#include<cstdio>
#include<deque>
using namespace std;
struct node
{
int id,val;
}w[1000005];
int minn[1000005],maxx[1000005];
int n,k;
deque <node> MIN;
deque <node> MAX;
int main()
{
freopen("window.in","r",stdin);
freopen("window.out","w",stdout);
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&w[i].val);
w[i].id=i;
}
for(int i=1;i<=k;i++)
{
if(MIN.empty())
{
MIN.push_back(w[i]);
}
else
{
while(!MIN.empty())
{
if(MIN.back().val>w[i].val)
{
MIN.pop_back();
continue;
}
break;
}
MIN.push_back(w[i]);
}
if(MAX.empty())
{
MAX.push_back(w[i]);
}
else
{
while(!MAX.empty())
{
if(MAX.back().val<w[i].val)
{
MAX.pop_back();
continue;
}
break;
}
MAX.push_back(w[i]);
}
}
int tail=1;
minn[tail]=MIN.front().val;
maxx[tail]=MAX.front().val;
for(int i=k+1;i<=n;i++)
{
tail++;
while(MIN.front().id<tail&&!MIN.empty())
{
MIN.pop_front();
}
while(MAX.front().id<tail&&!MAX.empty())
{
MAX.pop_front();
}
if(MIN.empty())
{
MIN.push_back(w[i]);
}
else
{
while(!MIN.empty())
{
if(MIN.back().val>w[i].val)
{
MIN.pop_back();
continue;
}
break;
}
MIN.push_back(w[i]);
}
if(MAX.empty())
{
MAX.push_back(w[i]);
}
else
{
while(!MAX.empty())
{
if(MAX.back().val<w[i].val)
{
MAX.pop_back();
continue;
}
break;
}
MAX.push_back(w[i]);
}
minn[tail]=MIN.front().val;
maxx[tail]=MAX.front().val;
}
for(int i=1;i<=tail;i++)
{
printf("%d",minn[i]);
if(i!=tail)printf(" ");
}
printf("\n");
for(int i=1;i<=tail;i++)
{
printf("%d",maxx[i]);
if(i!=tail) printf(" ");
}
//while(1);
}