-
Notifications
You must be signed in to change notification settings - Fork 0
/
even_pairs.cpp
61 lines (48 loc) · 1.33 KB
/
even_pairs.cpp
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
51
52
53
54
55
56
57
58
59
60
61
//
// even_pairs.cpp
// Algorithms Lab
//
// Created by Jonas Gessner
// Copyright © 2019 Jonas Gessner. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
static void runTestCase() {
int n;
cin >> n;
vector<int> numbers;
numbers.reserve(n);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
numbers.push_back(x);
}
int evenCount = 0;
int oddCount = 0;
int count = 0;
bool even = true;
for (int i = 0; i < n; i++) {
bool thisEven = (numbers[i] % 2) == 0;
even = even == thisEven;
// For every even partial sum, we cam create 1 even contigious subsequence from it, plus an additional number equal to the number of even paertial sums we already encountered (since the differences with will be even).
if (even) {
evenCount++;
count += evenCount;
} else {
// For every odd partial sum, we cam create he number of odd paertial sums we already encountered (since the differences with will be odd).
count += oddCount;
oddCount++;
}
}
cout << count << "\n";
}
int main(int argc, const char *argv[]) {
ios_base::sync_with_stdio(false);
int t;
cin >> t;
for (int i = 0; i < t; i++) {
runTestCase();
}
return 0;
}