題目:
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 48443 | Accepted: 13988 | |
Case Time Limit: 5000MS |
Description
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
Output
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
Source
題意:給一個長度爲n的序列,求出其中每個長度爲k的區間的最大值和最小值。
思路:我們可以用兩個單調隊列來分別維護最大值和最小值,先使前k個元素入隊,找到其中的最大值和最小值(即隊首元素),然後把後面的元素一次入隊,每一個元素入隊就代表了一個新的長度爲k的區間,然後我們找出跟當前位置的距離在k以內的隊首保存下來即可。
代碼:
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}
/*#ifdef HOME
freopen("in.txt","r",stdin);
#endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME
int Scan()
{
int res = 0, ch, flag = 0;
if((ch = getchar()) == '-') //判斷正負
flag = 1;
else if(ch >= '0' && ch <= '9') //得到完整的數
res = ch - '0';
while((ch = getchar()) >= '0' && ch <= '9' )
res = res * 10 + ch - '0';
return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/
struct node
{
int num;
int po;
};
int a[1000000+5];
node q1[1000000+5];
node q2[1000000+5];
int ans1[1000000+5];
int ans2[1000000+5];
int main()
{int n,k;
RII(n,k);
for(int i=0;i<n;i++)
RI(a[i]);
int front1=0,rear1=0,front2=0,rear2=0;
for(int i=0;i<k&&i<n;i++)
{while(front1<rear1&&q1[rear1-1].num>=a[i])
rear1--;
q1[rear1].num=a[i];
q1[rear1++].po=i;
while(front2<rear2&&q2[rear2-1].num<=a[i])
rear2--;
q2[rear2].num=a[i];
q2[rear2++].po=i;
}
int p1=0,p2=0;
ans1[p1++]=q1[front1].num;
ans2[p2++]=q2[front2].num;
for(int i=k;i<n;i++)
{
while(front1<rear1&&q1[rear1-1].num>=a[i])
rear1--;
q1[rear1].num=a[i];
q1[rear1++].po=i;
while(front2<rear2&&q2[rear2-1].num<=a[i])
rear2--;
q2[rear2].num=a[i];
q2[rear2++].po=i;
while(i-q1[front1].po>=k)
front1++;
ans1[p1++]=q1[front1].num;
while(i-q2[front2].po>=k)
front2++;
ans2[p2++]=q2[front2].num;
}
for(int i=0;i<p1-1;i++)
printf("%d ",ans1[i]);
printf("%d\n",ans1[p1-1]);
for(int i=0;i<p2-1;i++)
printf("%d ",ans2[i]);
printf("%d\n",ans2[p2-1]);
return 0;
}