POJ 2823 Sliding Window
生活随笔
收集整理的這篇文章主要介紹了
POJ 2823 Sliding Window
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Sliding Window 鏈接:http://poj.org/problem?id=2823
The array is?[1?3?-1?-3?5?3?6?7], and?k?is 3.
數列長度:N<=106,m<=N #include<iostream> #include<cstdio> using namespace std;const int N = 1000005;int n,k,m,mn[N],mx[N],n1,n2;struct node{int id, data; };node dj[N],dz[N];//dz 遞增隊列 dj 遞減隊列 int main() {cin >> n >> k;int headj = 1,tail1 = 0,headz = 1,tail2 = 0;for(int i = 1; i <= n; i++){scanf("%d",&m);while(m <= dj[tail1].data && tail1 >= headj) tail1 --;//保持單調性 while(m >= dz[tail2].data && tail2 >= headz) tail2 --;dj[++tail1].data = m;dj[tail1].id = i;dz[++tail2].data = m;dz[tail2].id = i;if(i >= k){//開始記錄第n段中的最值 if(dj[headj].id <= i-k) mn[++n1] = dj[++headj].data;//過期了 else mn[++n1] = dj[headj].data;if(dz[headz].id <= i-k) mx[++n2] = dz[++headz].data;else mx[++n2] = dz[headz].data;}}for(int i = 1; i <= n1; i++)printf("%d ",mn[i]);cout<<endl;for(int i = 1; i <= n1; i++)printf("%d ",mx[i]);return 0; }
| Time Limit:?12000MS | ? | Memory Limit:?65536K |
| ? | ? | ? |
| 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.
| [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 7Sample Output
-1 -3 -3 -3 3 3 3 3 5 5 6 7Source
POJ Monthly--2006.04.28, Ikki 題意:給定一個數列,從左至右輸出每個長度為m的數列段內的最小數和最大數。數列長度:N<=106,m<=N #include<iostream> #include<cstdio> using namespace std;const int N = 1000005;int n,k,m,mn[N],mx[N],n1,n2;struct node{int id, data; };node dj[N],dz[N];//dz 遞增隊列 dj 遞減隊列 int main() {cin >> n >> k;int headj = 1,tail1 = 0,headz = 1,tail2 = 0;for(int i = 1; i <= n; i++){scanf("%d",&m);while(m <= dj[tail1].data && tail1 >= headj) tail1 --;//保持單調性 while(m >= dz[tail2].data && tail2 >= headz) tail2 --;dj[++tail1].data = m;dj[tail1].id = i;dz[++tail2].data = m;dz[tail2].id = i;if(i >= k){//開始記錄第n段中的最值 if(dj[headj].id <= i-k) mn[++n1] = dj[++headj].data;//過期了 else mn[++n1] = dj[headj].data;if(dz[headz].id <= i-k) mx[++n2] = dz[++headz].data;else mx[++n2] = dz[headz].data;}}for(int i = 1; i <= n1; i++)printf("%d ",mn[i]);cout<<endl;for(int i = 1; i <= n1; i++)printf("%d ",mx[i]);return 0; }
第一次手打隊列,以后還是用雙向吧
轉載于:https://www.cnblogs.com/EdSheeran/p/8406551.html
總結
以上是生活随笔為你收集整理的POJ 2823 Sliding Window的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【深度学习系列】迁移学习Transfer
- 下一篇: Linux 配置jdk