码迷,mamicode.com
首页 > Windows程序 > 详细

poj 2823 Sliding Window

时间:2017-10-12 19:23:15      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:strong   using   cst   --   code   const   namespace   bsp   space   

经典题

类似单调队列,好像不能用stl里的队列直接模拟QAQ

Codes:

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 
 6 using namespace std;
 7 const int N = 1000000 + 5;
 8 
 9 int cnt1,cnt2,n,k,a[N],cn;
10 int q1[N],q2[N],minn[N],maxx[N];
11 int ans1[N],ans2[N];
12 int main(){
13     scanf("%d%d",&n,&k);
14     int head1,tail1,head2,tail2;
15     head1 = tail1 = head2 = tail2 = 0;
16     for(int i = 1;i <= n;++ i){
17         scanf("%d",&a[i]);
18         while(head1 <= tail1 && a[i] < a[q1[tail1]]) {
19             tail1 --;
20         }
21             
22         q1[++ tail1] = i;
23         
24         while(q1[head1] < i - k + 1)    
25             head1 ++;
26         if(i >= k) 
27             minn[++ cn] = q1[head1];
28         while(head2 <= tail2 && a[i] > a[q2[tail2]]) 
29             tail2 --;
30         q2[++ tail2] = i;
31         while(q2[head2] < i - k + 1) 
32             head2 ++;
33         if(i >= k)
34             maxx[cn] = q2[head2];
35     }
36     for(int i = 1;i <= cn;++ i) 
37         if(i == n) 
38             cout << a[minn[i]]; 
39         else 
40             cout << a[minn[i]] << " ";
41     cout << \n;
42     for(int i = 1;i <= cn;++ i) 
43         if(i == n) 
44             cout << a[maxx[i]]; 
45         else 
46             cout << a[maxx[i]] << " ";
47     return 0;
48 }

 

poj 2823 Sliding Window

标签:strong   using   cst   --   code   const   namespace   bsp   space   

原文地址:http://www.cnblogs.com/Loizbq/p/7657412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!