-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday39.cpp
More file actions
46 lines (36 loc) · 1.26 KB
/
day39.cpp
File metadata and controls
46 lines (36 loc) · 1.26 KB
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
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int main() {
int T; // Number of test cases
cin >> T;
while (T--) {
int n, k; // Number of locations and required number of oases
cin >> n >> k;
vector<int> nums(n);
for (int i = 0; i < n; ++i) {
cin >> nums[i];
}
// Transform nums into a binary array (1 for odd, 0 for even)
for (int i = 0; i < n; ++i) {
nums[i] = nums[i] % 2;
}
// Use prefix sum and hashmap for counting pleasant paths
unordered_map<int, int> prefixCount;
prefixCount[0] = 1; // Initial prefix sum is 0 (base case)
int prefixSum = 0, pleasantPaths = 0;
for (int i = 0; i < n; ++i) {
prefixSum += nums[i];
// Check if there exists a previous prefix sum equal to (prefixSum - k)
if (prefixCount.find(prefixSum - k) != prefixCount.end()) {
pleasantPaths += prefixCount[prefixSum - k];
}
// Increment the count of the current prefix sum
prefixCount[prefixSum]++;
}
// Output the number of pleasant paths for this test case
cout << pleasantPaths << endl;
}
return 0;
}