越来越水了。。。
这道题是简单的单调队列,同时维护最大值和最小值即可。
另解:multiset大法求区间最大最小,但是复杂度会上升。。。
1 /************************************************************** 2 Problem: 1342 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:944 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 for (i = 2; i <= n; ++i){ 51 while (q1[h1] + m <= i) ++h1; 52 while (h1 <= t1 && a[q1[t1]] <= a[i]) --t1; 53 q1[++t1] = i; 54 while (q2[h2] + m <= i) ++h2; 55 while (h2 <= t2 && a[q2[t2]] >= a[i]) --t2; 56 q2[++t2] = i; 57 if (check(i)){ 58 print(i - m + 1); 59 f = 1; 60 } 61 } 62 if (!f) puts("NONE"); 63 return 0; 64 }
(p.s. 那个300ms的这是怎么做到的。。。我输入输出外挂都开了好不好。。。哭T T)
时间: 2024-10-23 19:08:28