Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
34122 gaochunzhen 【S】T1 C++ 通过 100 974 MS 4172 KB 1642 2024-11-03 15:09:33

Tests(10/10):


#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 5e5 + 9; int n, m, a[N], cnt[N]; int q1[N], h1, t1, q2[N], h2, t2; int work(int L) { h1 = h2 = 1, t1 = t2 = 0; int res = 2e9; for (int i = 1; i <= n; i++) { while (h1 <= t1 && a[q1[t1]] <= a[i]) t1--; while (h2 <= t2 && a[q2[t2]] >= a[i]) t2--; q1[++t1] = i, q2[++t2] = i; if (i >= L) { res = min(res, a[q1[h1]] - a[q2[h2]]); while (h1 <= t1 && q1[h1] <= i - L + 1) h1++; while (h2 <= t2 && q2[h2] <= i - L + 1) h2++; } } return res; } void slv() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); cnt[i] = 0; } int X = work(n - m + 1), pt = 1; printf("%d ", X); h1 = h2 = 1, t1 = t2 = 0; for (int i = 1; i <= n; i++) { while (pt <= n && (h1 > t1 || h2 > t2 || a[q1[h1]] - a[q2[h2]] < X)) { while (h1 <= t1 && a[q1[t1]] <= a[pt]) t1--; while (h2 <= t2 && a[q2[t2]] >= a[pt]) t2--; q1[++t1] = pt, q2[++t2] = pt, pt++; } if (h1 > t1 || h2 > t2 || a[q1[h1]] - a[q2[h2]] < X) break; cnt[pt - i]++, cnt[n - i + 2]--; while (h1 <= t1 && q1[h1] <= i) h1++; while (h2 <= t2 && q2[h2] <= i) h2++; } for (int i = 1; i <= n; i++) { cnt[i] += cnt[i - 1]; if (cnt[i] >= m) { printf("%d\n", i); return; } } printf("-1\n"); } signed main() { int T; scanf("%d", &T); while (T--) slv(); return 0; }


测评信息: