POJ - 2823 Sliding Window (单调队列)

分别维护一个单调递减的队列和单调递增队列就可以了,但是这题莫名的坑,要用C++提交卡编译器的时间才能AC,真的是日了狗了,以后就用C++提交好了,不用别的了。

 1 #include <iostream>
 2 #include <queue>
 3 #include <stack>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <bitset>
 9 #include <algorithm>
10 #include <cmath>
11 #include <cstring>
12 #include <cstdlib>
13 #include <string>
14 #include <sstream>
15 #include <time.h>
16 #define x first
17 #define y second
18 #define pb push_back
19 #define mp make_pair
20 #define lson l,m,rt*2
21 #define rson m+1,r,rt*2+1
22 #define mt(A,B) memset(A,B,sizeof(A))
23 using namespace std;
24 typedef long long LL;
25 //const double PI = acos(-1);
26 const int N=1e6+10;
27 const LL mod=1e9+7;
28 const int inf = 0x3f3f3f3f;
29 const LL INF=0x3f3f3f3f3f3f3f3fLL;
30 int a[N],MI[N],MA[N];
31 struct node
32 {
33     int x,y;
34 }Q[N];//第一位是下标,第二位是数据
35 int main()
36 {
37 #ifdef Local
38     freopen("data.txt","r",stdin);
39 #endif
40     //ios::sync_with_stdio(false);
41     //cin.tie(0);
42     int n,k,w=0;
43     register int head=0,tail=0;
44     scanf("%d%d",&n,&k);
45     for(int i=0;i<n;i++)scanf("%d",&a[i]);
46     for(int i=0;i<n;i++)
47     {
48         while(tail-head>0&&Q[head+1].x<i-k+1)head++;
49         while(tail>head&&Q[tail].y>=a[i])tail--;
50         tail++;
51         Q[tail].y=a[i];
52         Q[tail].x=i;
53         if(i>=k-1)MI[w++]=Q[head+1].y;
54     }
55     head=0;tail=0;w=0;
56     for(int i=0;i<n;i++)
57     {
58         while(tail-head>0&&Q[head+1].x<i-k+1)head++;
59         while(tail>head&&Q[tail].y<=a[i])tail--;
60         tail++;
61         Q[tail].y=a[i];
62         Q[tail].x=i;
63         if(i>=k-1)MA[w++]=Q[head+1].y;
64     }
65     for(int i=0;i<w;i++)
66     {
67         if(i==w-1)printf("%d\n",MI[i]);
68         else printf("%d ",MI[i]);
69     }
70     for(int i=0;i<w;i++)
71     {
72         if(i==w-1)printf("%d\n",MA[i]);
73         else printf("%d ",MA[i]);
74     }
75 #ifdef Local
76     cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
77 #endif
78 }
View Code

 

posted @ 2017-03-04 23:57  Kcl886  阅读(120)  评论(0编辑  收藏  举报