#include <iostream> #include <vector> using namespace std; const int mini = -10000000; const int maxi = 10000000; int continue_assuming_0_1(vector<int>& v) { int n = v.size(); int res = 0; for(int i = 2; i < n; i++) { bool malec = v[i-1] > v[i-2]; if(malec) { if(v[i] < v[i-1]) { // ok } else { v[i] = mini; res++; } } else { if(v[i] > v[i-1]) { // ok } else { v[i] = maxi; res++; } } } return res; } int main() { int n; cin >> n; vector<int> v(n); for(int i = 0; i < n; i++) cin >> v[i]; int best = 20000000; if(v[0] != v[1]) { // pierwsze 2 zostaja vector<int> v1 = v; int res = continue_assuming_0_1(v1); if(res < best) best = res; } vector<int> v1 = v; v1[1] = mini; int res = continue_assuming_0_1(v1) + 1; if(res < best) best = res; v1 = v; v1[1] = maxi; res = continue_assuming_0_1(v1) + 1; if(res < best) best = res; cout << best << endl; return 0; }
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 62 63 64 | #include <iostream> #include <vector> using namespace std; const int mini = -10000000; const int maxi = 10000000; int continue_assuming_0_1(vector<int>& v) { int n = v.size(); int res = 0; for(int i = 2; i < n; i++) { bool malec = v[i-1] > v[i-2]; if(malec) { if(v[i] < v[i-1]) { // ok } else { v[i] = mini; res++; } } else { if(v[i] > v[i-1]) { // ok } else { v[i] = maxi; res++; } } } return res; } int main() { int n; cin >> n; vector<int> v(n); for(int i = 0; i < n; i++) cin >> v[i]; int best = 20000000; if(v[0] != v[1]) { // pierwsze 2 zostaja vector<int> v1 = v; int res = continue_assuming_0_1(v1); if(res < best) best = res; } vector<int> v1 = v; v1[1] = mini; int res = continue_assuming_0_1(v1) + 1; if(res < best) best = res; v1 = v; v1[1] = maxi; res = continue_assuming_0_1(v1) + 1; if(res < best) best = res; cout << best << endl; return 0; } |