QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#90956 | #6130. Plants vs. Zombies | zzh00195 | AC ✓ | 414ms | 4784kb | C++14 | 1.5kb | 2023-03-26 10:42:10 | 2023-03-26 10:42:11 |
Judging History
answer
#include <bits/stdc++.h>
#define MAXN ((int) 1e5)
using namespace std;
int n, A[MAXN + 10];
long long m;
// 表示目前每棵植物的高度
long long B[MAXN + 10];
bool check(long long LIM) {
long long step = 0;
memset(B, 0, sizeof(long long) * (n + 3));
for (int i = 1; i <= n; i++) {
if (LIM > B[i]) {
// 第 i 棵植物需要继续浇水
// 在它和第 (i + 1) 棵植物之间反复移动
long long t = LIM - B[i];
t = (t + A[i] - 1) / A[i];
step += t * 2 - 1;
// 步数超出限制就即刻退出
// 否则数据范围可能超出 long long
if (step > m) return false;
B[i + 1] += A[i + 1] * (t - 1);
} else {
// 第 i 棵植物不需要继续浇水
// 直接路过,但步数还要算
// 这里步数可能会超过限制
// 但是只要右边没有其它需要浇水的植物,就不会返回 false
step++;
}
}
return true;
}
void solve() {
scanf("%d%lld", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
// 二分答案
long long head = 0, tail = 1e18;
while (head < tail) {
long long mid = (head + tail + 1) >> 1;
if (check(mid)) head = mid;
else tail = mid - 1;
}
printf("%lld\n", head);
}
int main() {
int tcase; scanf("%d", &tcase);
while (tcase--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3704kb
input:
2 4 8 3 2 6 6 3 9 10 10 1
output:
6 4
result:
ok 2 number(s): "6 4"
Test #2:
score: 0
Accepted
time: 176ms
memory: 4784kb
input:
116 4 0 3 2 6 6 4 1 3 2 6 6 10 19 10 2 8 4 2 4 9 3 3 3 4 8 3 9 3 6 2 19 2 10 11 15 3 1 1 4 3 7 10 8 6 7 10 10 14 8 7 1 1 10 9 2 8 10 7 2 13 2 3 10 10 8 1 6 6 9 4 7 1 8 8 7 14 6 7 4 5 3 1 3 11 6 8 1 10 9 7 2 6 6 1 3 9 4 10 6 1 3 8 7 7 10 6 2 10 4 7 2 5 11 9 10 5 9 2 9 1 2 4 8 6 2 8 8 1 6 4 5 7 2 9 8 ...
output:
0 0 4 6 20 3 2 14 1 4 0 4 2 6 0 2 24 3 30 10 0 2 3 0 0 2 6 0 1 6 24 28 1 4 3 0 4 10 6 4 1 5 0 2 0 7 30 2 0 0 16 8 0 30 2 30 4 2 0 0 2 0 2 0 5 2 0 0 0 0 5 0 4 6 28 0 0 21 3 0 2 4 0 0 14 4 6 1 0 5 14 3 8 0 4 0 10 12 5 8 1 6 12 0 725 0 17 132 676 588 110 0 163 5910826 1000000000000 16561021906
result:
ok 116 numbers
Test #3:
score: 0
Accepted
time: 414ms
memory: 4708kb
input:
2110 4 0 3 2 6 6 4 1 3 2 6 6 2 7 3 9 28 82 68 27 32 98 88 19 96 37 53 50 45 37 41 92 51 70 73 84 2 53 7 89 9 59 24 23 62 97 22 145 61 22 87 34 75 89 22 6 10 74 21 35 33 23 85 66 89 46 16 48 2 48 25 146 67 63 93 98 1 70 57 45 40 15 1 5 54 38 96 19 94 18 43 98 28 45 47 92 46 21 26 83 54 21 72 25 47 2 ...
output:
0 0 12 35 72 29 6 125 1462 93 160 54 28 27 4 33 625 43 60 1056 0 116 42 31 66 0 0 370 80 198 66 189 39 55 42 36 63 810 325 36 38 20 59 115 0 0 25 4 7 0 72 0 60 1045 39 24 48 35 120 16 86 42 0 28 38 135 67 22 60 104 0 2256 110 0 0 4 25 59 49 0 16 1008 0 26 750 36 33 12 13 48 48 85 18 3135 182 24 160 ...
result:
ok 2110 numbers