-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBouncing Ball
50 lines (45 loc) Β· 1.28 KB
/
Bouncing Ball
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <numeric>
#include <cmath>
class Analyzer {
std::vector<int> sequence, prefixSum, suffixSum;
void calculateSums() {
prefixSum = suffixSum = sequence;
std::partial_sum(sequence.begin(), sequence.end(), prefixSum.begin());
std::partial_sum(sequence.rbegin(), sequence.rend(), suffixSum.rbegin());
}
int findOptions() {
int options = 0;
for (size_t dx = 0; dx < sequence.size(); ++dx) {
if (sequence[dx] == 0) {
int leftSum = dx ? prefixSum[dx - 1] : 0;
int rightSum = dx < sequence.size() - 1 ? suffixSum[dx + 1] : 0;
options += (leftSum == rightSum) ? 2 : (std::abs(leftSum - rightSum) == 1);
}
}
return options;
}
public:
void handleTest() {
int length;
std::cin >> length;
sequence.resize(length);
for (int &value : sequence) {
std::cin >> value;
}
calculateSums();
std::cout << findOptions() << '\n';
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int testCases;
std::cin >> testCases;
Analyzer analyzer;
while (testCases--) {
analyzer.handleTest();
}
return 0;
}