POJ 2823Sliding Window(單調佇列水題)
Sliding Window
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 33362 | Accepted: 9918 | |
Case Time Limit: 5000MS |
Description
An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window
moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
Window position | Minimum value | Maximum value |
---|---|---|
[1 3 -1] -3 5 3 6 7 | -1 | 3 |
1 [3 -1 -3] 5 3 6 7 | -3 | 3 |
1 3 [-1 -3 5] 3 6 7 | -3 | 5 |
1 3 -1 [-3 5 3] 6 7 | -3 | 5 |
1 3 -1 -3 [5 3 6] 7 | 3 | 6 |
1 3 -1 -3 5 [3 6 7] | 3 | 7 |
Your task is to determine the maximum and minimum values in the sliding window at each position.
Input
The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.
Output
There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.
Sample Input
8 3 1 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 3 3 3 5 5 6 7
題目意思就不多說了,直接用單調佇列,自己沒寫輸入優化時間還少一點,不過都需要好5s+。單調佇列模板題目,純屬娛樂。。預祝明天8086能在南京現場賽上取得好成績,fighting!!!!
題目地址:Sliding Window
AC程式碼:
#include<iostream>
#include<cstdio>
using namespace std;
struct node
{
int num;
int i;
}q1[1000006],q2[1000006];
int res1[1000006],res2[1000006];
int getint()
{
int ans=0;
int flag=0;
char ch=getchar();
while(ch<'0'||ch>'9'||ch=='-')
{
if(ch=='-') flag=1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
ans=ans*10+(ch-'0');
ch=getchar();
}
if(flag) ans=-ans;
return ans;
}
int main()
{
int n,k,i;
while(cin>>n>>k)
{
int top1,top2,tail1,tail2;
top1=top2=tail1=tail2=0;
int p;
for(i=0;i<n;i++)
{
//p=getint();
scanf("%d",&p);
while(top1<tail1&&q1[tail1-1].num<p) tail1--;
q1[tail1].i=i,q1[tail1++].num=p;
while(i-q1[top1].i>=k) top1++;
while(top2<tail2&&q2[tail2-1].num>p) tail2--;
q2[tail2].i=i,q2[tail2++].num=p;
while(i-q2[top2].i>=k) top2++;
if(i>=k-1) res1[i-k+1]=q1[top1].num,res2[i-k+1]=q2[top2].num;
}
printf("%d",res2[0]);
for(i=1;i<=n-k;i++)
printf(" %d",res2[i]);
printf("\n%d",res1[0]);
for(i=1;i<=n-k;i++)
printf(" %d",res1[i]);
printf("\n");
}
return 0;
}
/*
8 3
-1 2 3 1 3 5 6 7
8 3
1 3 -1 -3 5 3 6 7
*/
//5344MS 未讀入優化
//7079MS 優化輸入之後。。。
相關文章
- POJ 2823 單調佇列佇列
- POJ2823 Sliding Window (單調佇列的基本應用)佇列
- POJ 3017 單調佇列dp佇列
- poj3017 dp+單調佇列佇列
- 單調佇列佇列
- 單調棧/單調佇列佇列
- 單調佇列雙端佇列佇列
- 單調棧 和 單調佇列佇列
- 單調棧和單調佇列佇列
- hdu 3415 單調佇列佇列
- 多重揹包問題的單調佇列優化佇列優化
- HDU 3530 單調佇列佇列
- hdu 3401 單調佇列+DP佇列
- hdu 4122 單調佇列佇列
- 單調佇列最佳化 DP佇列
- hdu4374單調佇列+dp佇列
- 佇列-單端佇列佇列
- POJ 3253 Fence Repair 優先佇列AI佇列
- POJ 2259 Team Queue【模擬佇列】佇列
- POJ 1724 ROADS(優先佇列+spfa)佇列
- POJ2431 Expedition (優先佇列)佇列
- POJ 2051(最小堆/優先佇列)佇列
- noi.ac #289. 電梯(單調佇列)佇列
- HDU 3530 Subsequence (dp+單調佇列)佇列
- HDU3530 單調佇列的應用佇列
- 優先佇列的學習記錄--例題:Expedition(POJ2431)佇列
- C語言 簡單的佇列(陣列佇列)C語言佇列陣列
- hdu3415 單調佇列求區間最大和佇列
- hdu 4122Alice's mooncake shop(單調佇列)佇列
- [學習筆記] 單調佇列最佳化DP - DP筆記佇列
- kuangbin 專題二十三:二分 尺取 單調棧佇列 String佇列
- [POJ 3415] Common Substrings (字尾陣列+單調棧優化)陣列優化
- Poj--3080Blue Jeans+KMP水題KMP
- POJ 3566 Building for UN (無腦構造 水題)UI
- POJ 3253Fence Repair(哈夫曼&優先佇列)AI佇列
- 洛谷題單指南-常見最佳化技巧-P1886 滑動視窗 /【模板】單調佇列佇列
- RabbitMQ-簡單佇列MQ佇列
- 單向鏈式佇列佇列