forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpivot.cpp
53 lines (43 loc) · 930 Bytes
/
pivot.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
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v;
vector<int> c_max;
vector<int> c_min;
v.resize(n);
c_max.resize(n);
c_min.resize(n);
// Take in the data
for(auto& i : v) {
cin >> i;
}
// Calculate max from left
c_max[0] = v[0];
for(int i = 1; i < v.size(); i++) {
c_max[i] = max(v[i], c_max[i-1]);
}
// Calculate min from right
c_min[n-1] = v[n-1];
for(int i = n-2; i >= 0; i--) {
c_min[i] = min(v[i], c_min[i+1]);
}
// Count edge pivots
int total = 0;
if(v[0] < c_min[1]) {
total++;
}
if(v[n-1] > c_min[n-2]) {
total++;
}
// Count center pivots
for(int i = 1; i < n-1; i++) {
if(v[i] < c_min[i+1] && v[i] > c_max[i-1]) {
total++;
}
}
// Print answer
cout << total << endl;
}