链接:
http://poj.org/problem?id=2823
题意:
给定一个长度为n的数列,a0,a1,···,an-1和一个整数k。
求数列bi=min{ai,ai+1,···,ai+k-1}(i=0,1,···,n-k)
和数列ci=max{ai,ai+1,···,ai+k-1}(i=0,1,···,n-k)
代码:
31 int a[MAXN], b[MAXN], c[MAXN]; 32 deque<int> deq; 33 34 int main() { 35 int n, k; 36 cin >> n >> k; 37 rep(i, 0, n) scanf("%d", a + i); 38 39 rep(i, 0, n) { 40 while (!deq.empty() && a[*deq.rbegin()] >= a[i]) deq.pop_back(); 41 deq.push_back(i); 42 if (i - k + 1 >= 0) { 43 b[i - k + 1] = a[*deq.begin()]; 44 if (*deq.begin() == i - k + 1) deq.pop_front(); 45 } 46 } 47 deq.clear(); 48 rep(i, 0, n) { 49 while (!deq.empty() && a[*deq.rbegin()] <= a[i]) deq.pop_back(); 50 deq.push_back(i); 51 if (i - k + 1 >= 0) { 52 c[i - k + 1] = a[*deq.begin()]; 53 if (*deq.begin() == i - k + 1) deq.pop_front(); 54 } 55 } 56 57 rep(i, 0, n - k + 1) printf("%d%c", b[i], i == n - k ? ‘\n‘ : ‘ ‘); 58 rep(i, 0, n - k + 1) printf("%d%c", c[i], i == n - k ? ‘\n‘ : ‘ ‘); 59 return 0; 60 }
时间: 2024-11-09 09:38:58