Skip to content

Commit

Permalink
Solved ARRAYSUB. Sliding window maximum.
Browse files Browse the repository at this point in the history
  • Loading branch information
natansh committed Oct 21, 2012
1 parent be9e85f commit d899802
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ARRAYSUB/input.in
@@ -0,0 +1,3 @@
9
1 2 3 1 4 5 2 3 6
2
4 changes: 4 additions & 0 deletions ARRAYSUB/input2.in
@@ -0,0 +1,4 @@
10

10 9 4 7 8 5 4 3 2 1
5
41 changes: 41 additions & 0 deletions ARRAYSUB/source.cpp
@@ -0,0 +1,41 @@
#include <cstdio>
#include <queue>
#include <utility>

const int max_n = 1000010;
int num[max_n];

int main() {
int n;
scanf("%d", &n);

for(int i = 0; i < n; ++i) {
scanf("%d", &num[i]);
}

int k;
scanf("%d", &k);

bool first_case = true;
std::deque<int> q;
for(int i = 0; i < n; ++i) {
if(i >= k) {
if(!q.empty() && q.front() <= i - k) {
q.pop_front();
}
}
while(!q.empty() && num[i] >= num[q.back()]) {
q.pop_back();
}
q.push_back(i);
if(i >= k - 1) {
if(!first_case) {
printf(" ");
} else {
first_case = false;
}
printf("%d", num[q.front()]);
}
}
printf("\n");
}

0 comments on commit d899802

Please sign in to comment.