越来越水了。。。
这道题是简单的单调队列,同时维护最大值和最小值即可。
另解:multiset大法求区间最大最小,但是复杂度会上升。。。
1 /************************************************************** 2 Problem: 1342 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:916 ms 7 Memory:12524 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 12 using namespace std; 13 const int N = 1000005; 14 int n, m, C, a[N]; 15 int q1[N], q2[N], h1, h2, t1, t2; 16 bool f; 17 18 inline int read(){ 19 int x = 0; 20 char ch = getchar(); 21 while (ch < '0' || ch > '9') 22 ch = getchar(); 23 while (ch >= '0' && ch <= '9'){ 24 x = x * 10 + ch - '0'; 25 ch = getchar(); 26 } 27 return x; 28 } 29 30 int pr[10], NUM = 0; 31 inline int print(int x){ 32 while (x) 33 pr[++NUM] = (x % 10) + '0', x /= 10; 34 while (NUM) 35 putchar(pr[NUM--]); 36 putchar('\n'); 37 } 38 39 inline bool check(int i){ 40 return a[q1[h1]] - a[q2[h2]] <= C && i >= m; 41 } 42 43 int main(){ 44 n = read(), m = read(), C = read(); 45 int i; 46 for (i = 1; i <= n; ++i) 47 a[i] = read(); 48 f = 0; 49 q1[1] = q2[1] = 1, h1 = t1 = h2 = t2 = 1; 50 if (check(1)){ 51 print(1); 52 f = 1; 53 } 54 for (i = 2; i <= n; ++i){ 55 while (q1[h1] + m <= i) ++h1; 56 while (h1 <= t1 && a[q1[t1]] <= a[i]) --t1; 57 q1[++t1] = i; 58 while (q2[h2] + m <= i) ++h2; 59 while (h2 <= t2 && a[q2[t2]] >= a[i]) --t2; 60 q2[++t2] = i; 61 if (check(i)){ 62 print(i - m + 1); 63 f = 1; 64 } 65 } 66 if (!f) puts("NONE"); 67 return 0; 68 }
(p.s. 那个300ms的这是怎么做到的。。。我输入输出外挂都开了好不好。。。哭T T)
By Xs酱~ 转载请说明
博客地址:http://www.cnblogs.com/rausen