#include <iostream> #include <vector> #define MAX 1000000000 #define MIN -1000000000 using namespace std; vector<int> ascending, descending; int solution(vector<int>&, bool, int); int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) { int next; cin >> next; ascending.push_back(next); descending.push_back(next); } int aedits = solution(ascending, true, n); int dedits = solution(descending, false, n); cout << (aedits < dedits ? aedits : dedits); return 0; } int solution(vector<int>& store, bool dir, int size) { int ret = 0; for (int i = 1; i < size; i++) { if (dir ? (store[i - 1] >= store[i]) : store[i - 1] <= store[i]) { store[i] = dir ? MAX : MIN; ret++; } dir = !dir; } return ret; }
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> #define MAX 1000000000 #define MIN -1000000000 using namespace std; vector<int> ascending, descending; int solution(vector<int>&, bool, int); int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) { int next; cin >> next; ascending.push_back(next); descending.push_back(next); } int aedits = solution(ascending, true, n); int dedits = solution(descending, false, n); cout << (aedits < dedits ? aedits : dedits); return 0; } int solution(vector<int>& store, bool dir, int size) { int ret = 0; for (int i = 1; i < size; i++) { if (dir ? (store[i - 1] >= store[i]) : store[i - 1] <= store[i]) { store[i] = dir ? MAX : MIN; ret++; } dir = !dir; } return ret; } |