洛谷P2032题解
入门题。
仔细浏览题面,发现是滑动窗口的超级弱化版。
只需要搞一个单调队列维护最大值就可以了。
1 #include<stdio.h> 2 #define reg register 3 #define ri reg int 4 #define rep(i, x, y) for(ri i = x; i <= y; ++i) 5 #define nrep(i, x, y) for(ri i = x; i >= y; --i) 6 #define DEBUG 1 7 #define ll long long 8 #define max(i, j) (i) > (j) ? (i) : (j) 9 #define min(i, j) (i) < (j) ? (i) : (j) 10 #define read(i) io.READ(i) 11 #define print(i) io.WRITE(i) 12 #define push(i) io.PUSH(i) 13 struct IO { 14 #define MAXSIZE (1 << 20) 15 #define isdigit(x) (x >= '0' && x <= '9') 16 char buf[MAXSIZE], *p1, *p2; 17 char pbuf[MAXSIZE], *pp; 18 #if DEBUG 19 #else 20 IO() : p1(buf), p2(buf), pp(pbuf) {} 21 ~IO() { 22 fwrite(pbuf, 1, pp - pbuf, stdout); 23 } 24 #endif 25 inline char gc() { 26 #if DEBUG 27 return getchar(); 28 #endif 29 if(p1 == p2) 30 p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin); 31 return p1 == p2 ? ' ' : *p1++; 32 } 33 inline bool blank(char ch) { 34 return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; 35 } 36 template <class T> 37 inline void READ(T &x) { 38 register double tmp = 1; 39 register bool sign = 0; 40 x = 0; 41 register char ch = gc(); 42 for(; !isdigit(ch); ch = gc()) 43 if(ch == '-') sign = 1; 44 for(; isdigit(ch); ch = gc()) 45 x = x * 10 + (ch - '0'); 46 if(ch == '.') 47 for(ch = gc(); isdigit(ch); ch = gc()) 48 tmp /= 10.0, x += tmp * (ch - '0'); 49 if(sign) x = -x; 50 } 51 inline void READ(char *s) { 52 register char ch = gc(); 53 for(; blank(ch); ch = gc()); 54 for(; !blank(ch); ch = gc()) 55 *s++ = ch; 56 *s = 0; 57 } 58 inline void READ(char &c) { 59 for(c = gc(); blank(c); c = gc()); 60 } 61 inline void PUSH(const char &c) { 62 #if DEBUG 63 putchar(c); 64 #else 65 if(pp - pbuf == MAXSIZE) { 66 fwrite(pbuf, 1, MAXSIZE, stdout); 67 pp = pbuf; 68 } 69 *pp++ = c; 70 #endif 71 } 72 template <class T> 73 inline void WRITE(T x) { 74 if(x < 0) { 75 x = -x; 76 PUSH('-'); 77 } 78 static T sta[35]; 79 T top = 0; 80 do { 81 sta[top++] = x % 10; 82 x /= 10; 83 }while(x); 84 while(top) 85 PUSH(sta[--top] + '0'); 86 } 87 template <class T> 88 inline void WRITE(T x, char lastChar) { 89 WRITE(x); 90 PUSH(lastChar); 91 } 92 } io; 93 int n, k, q[1000010], a[1000010]; 94 void getmax() { 95 ri head = 0, tail = 0; 96 rep(i, 1, k - 1) { 97 while(head <= tail && a[q[tail]] <= a[i]) --tail; 98 q[++tail] = i; 99 } 100 rep(i, k, n) { 101 while(head <= tail && a[q[tail]] <= a[i]) --tail; 102 q[++tail] = i; 103 while(q[head] <= i - k) ++head; 104 print(a[q[head]]); 105 puts(""); 106 } 107 } 108 int main() { 109 read(n), read(k); 110 rep(i, 1, n) read(a[i]); 111 getmax(); 112 return 0; 113 }