清北學堂模擬賽d6t2 刀塔
阿新 • • 發佈:2017-10-06
stream mem size can -s string using int str
分析:看到最小值最大就很顯然是二分了吧,二分一下最小值,把小於它的數給刪掉,然後看每個數向左邊能延伸多長,往右邊能延伸多長,最後統計一下有沒有可行答案就可以了.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int n, a, b, k, x[3000010], r, l, ans, lx[3000010], rx[3000010]; bool check(int u) { memset(lx, 0, sizeof(lx)); memset(rx, 0, sizeof(rx)); for (int i = 1; i <= n; i++) { if (x[i] >= u) lx[i] = lx[i - 1] + 1; else lx[i] = 0; } for (int i = n; i >= 1; i--) { if (x[i] >= u) rx[i] = rx[i + 1] + 1; else rx[i]= 0; } for (int i = 1; i <= n; i++) if (lx[i - 1] >= a && rx[i + k] >= a && lx[i - 1] + rx[i + k] >= b) return true; return false; } int main() { scanf("%d%d%d%d", &n, &a, &b, &k); for (int i = 1; i <= n; i++) { scanf("%d", &x[i]); r = max(r, x[i]); } l = 1; r++; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } printf("%d\n", ans); return 0; }
清北學堂模擬賽d6t2 刀塔