标签:war utc cstring ios 而不是 numbers enter sed tab
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 62915 | Accepted: 17956 | |
Case Time Limit: 5000MS |
Description
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
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 inline void read(int &x) 7 { 8 x = 0;char ch = getchar(), c = ch; 9 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar(); 10 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 11 if(c == ‘-‘)x = -x; 12 } 13 14 const int INF = 0x3f3f3f3f; 15 const int MAXN = 2000000 + 10; 16 const int MAXM = 2000000 + 10; 17 18 int num[MAXN],q[MAXN],rank[MAXN],head,tail,n,m; 19 20 int main() 21 { 22 read(n), read(m); 23 for(register int i = 1;i <= n;++ i) read(num[i]); 24 25 head = tail = 1, q[1] = num[1],rank[1] = 1; 26 27 //维护单调递增队列 28 for(register int i = 2;i <= m;++ i) 29 { 30 while(num[i] <= q[tail] && head <= tail) -- tail; 31 q[++tail] = num[i]; 32 rank[tail] = i; 33 } 34 printf("%d ", q[head]); 35 for(register int i = m + 1;i <= n;++ i) 36 { 37 while(num[i] <= q[tail] && head <= tail) -- tail; 38 while(i - rank[head] + 1 > m && head <= tail)++ head; 39 q[++tail] = num[i]; 40 rank[tail] = i; 41 printf("%d ", q[head]); 42 } 43 putchar(‘\n‘); 44 45 head = tail = 1, q[1] = num[1],rank[1] = 1; 46 47 //维护单调递减队列 48 for(register int i = 2;i <= m;++ i) 49 { 50 while(num[i] >= q[tail] && head <= tail) -- tail; 51 q[++tail] = num[i]; 52 rank[tail] = i; 53 } 54 printf("%d ", q[head]); 55 for(register int i = m + 1;i <= n;++ i) 56 { 57 while(num[i] >= q[tail] && head <= tail) -- tail; 58 while(i - rank[head] + 1 > m && head <= tail)++ head; 59 q[++tail] = num[i]; 60 rank[tail] = i; 61 printf("%d ", q[head]); 62 } 63 return 0; 64 }
标签:war utc cstring ios 而不是 numbers enter sed tab
原文地址:http://www.cnblogs.com/huibixiaoxing/p/7469801.html