#include <iostream> #include <algorithm> #include <vector> #define push push_back #define ll long long using namespace std; int proc(int n, vector<int> T, int trueVal, int falseVal) { // cout << "PROCEDURE\n"; vector<int> vertice(n+1); // -1, 0, 1 int result = 0; for(int i = 1; i < n; i++) { if(i%2 == trueVal) { if((vertice[i] == 1 || vertice[i-1] == -1) ){ continue; } if(T[i] >= T[i-1]) { // cout << i << " | " << " A\n"; result++; vertice[i] = 1; } }else if(i%2 == falseVal) { if((vertice[i] == -1 || vertice[i-1] == 1) ){ continue; } if(T[i] <= T[i-1]) { // cout << i << " | " << " B\n"; result++; vertice[i] = -1; } } } return result; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> T(n); for(int i = 0; i < n; i++) { cin >> T[i]; } int a = proc(n, T, 1, 0); int b = proc(n, T, 0, 1); // cout << a << " " << b << "\n"; cout << min(a,b) << "\n"; }
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 | #include <iostream> #include <algorithm> #include <vector> #define push push_back #define ll long long using namespace std; int proc(int n, vector<int> T, int trueVal, int falseVal) { // cout << "PROCEDURE\n"; vector<int> vertice(n+1); // -1, 0, 1 int result = 0; for(int i = 1; i < n; i++) { if(i%2 == trueVal) { if((vertice[i] == 1 || vertice[i-1] == -1) ){ continue; } if(T[i] >= T[i-1]) { // cout << i << " | " << " A\n"; result++; vertice[i] = 1; } }else if(i%2 == falseVal) { if((vertice[i] == -1 || vertice[i-1] == 1) ){ continue; } if(T[i] <= T[i-1]) { // cout << i << " | " << " B\n"; result++; vertice[i] = -1; } } } return result; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> T(n); for(int i = 0; i < n; i++) { cin >> T[i]; } int a = proc(n, T, 1, 0); int b = proc(n, T, 0, 1); // cout << a << " " << b << "\n"; cout << min(a,b) << "\n"; } |