#include <iostream> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); vector<int> tony; int n, zmianyA=0, zmianyB=0; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; tony.push_back(x); } vector<int> tonyA = tony; bool check = false; //wysoki for (int i = 1; i < n-1; i++) { if (check) { //powinien byc niski if (tonyA[i]>=tonyA[i-1]) { if (tonyA[i - 1] > tonyA[i + 1]) tonyA[i] = tonyA[i + 1] - 1; else tonyA[i] = tonyA[i - 1] - 1; zmianyA++; } } else { //powinien byc wysoki if (tonyA[i] <= tonyA[i - 1]) { if (tonyA[i - 1] < tonyA[i + 1]) tonyA[i] = tonyA[i + 1] + 1; else tonyA[i] = tonyA[i - 1] + 1; zmianyA++; } } check = !check; } if (check) { if (tonyA[n-1] >= tonyA[n-1 - 1]) { tonyA[n-1] = tonyA[n-1 - 1] - 1; zmianyA++; } } else { if (tonyA[n - 1] <= tonyA[n - 1 - 1]) { tonyA[n - 1] = tonyA[n - 1 - 1] + 1; zmianyA++; } } vector<int> tonyB = tony; check = true; //wysoki for (int i = 1; i < n - 1; i++) { if (check) { //powinien byc niski if (tonyB[i] >= tonyB[i - 1]) { if (tonyB[i - 1] > tonyB[i + 1]) tonyB[i] = tonyB[i + 1] - 1; else tonyB[i] = tonyB[i - 1] - 1; zmianyB++; } } else { //powinien byc wysoki if (tonyB[i] <= tonyB[i - 1]) { if (tonyB[i - 1] < tonyB[i + 1]) tonyB[i] = tonyB[i + 1] + 1; else tonyB[i] = tonyB[i - 1] + 1; zmianyB++; } } check = !check; } if (check) { if (tonyB[n - 1] >= tonyB[n - 1 - 1]) { tonyB[n - 1] = tonyB[n - 1 - 1] - 1; zmianyB++; } } else { if (tonyB[n - 1] <= tonyB[n - 1 - 1]) { tonyB[n - 1] = tonyB[n - 1 - 1] + 1; zmianyB++; } } if (zmianyA > zmianyB) cout << zmianyB; else cout << zmianyA; }
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include <iostream> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); vector<int> tony; int n, zmianyA=0, zmianyB=0; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; tony.push_back(x); } vector<int> tonyA = tony; bool check = false; //wysoki for (int i = 1; i < n-1; i++) { if (check) { //powinien byc niski if (tonyA[i]>=tonyA[i-1]) { if (tonyA[i - 1] > tonyA[i + 1]) tonyA[i] = tonyA[i + 1] - 1; else tonyA[i] = tonyA[i - 1] - 1; zmianyA++; } } else { //powinien byc wysoki if (tonyA[i] <= tonyA[i - 1]) { if (tonyA[i - 1] < tonyA[i + 1]) tonyA[i] = tonyA[i + 1] + 1; else tonyA[i] = tonyA[i - 1] + 1; zmianyA++; } } check = !check; } if (check) { if (tonyA[n-1] >= tonyA[n-1 - 1]) { tonyA[n-1] = tonyA[n-1 - 1] - 1; zmianyA++; } } else { if (tonyA[n - 1] <= tonyA[n - 1 - 1]) { tonyA[n - 1] = tonyA[n - 1 - 1] + 1; zmianyA++; } } vector<int> tonyB = tony; check = true; //wysoki for (int i = 1; i < n - 1; i++) { if (check) { //powinien byc niski if (tonyB[i] >= tonyB[i - 1]) { if (tonyB[i - 1] > tonyB[i + 1]) tonyB[i] = tonyB[i + 1] - 1; else tonyB[i] = tonyB[i - 1] - 1; zmianyB++; } } else { //powinien byc wysoki if (tonyB[i] <= tonyB[i - 1]) { if (tonyB[i - 1] < tonyB[i + 1]) tonyB[i] = tonyB[i + 1] + 1; else tonyB[i] = tonyB[i - 1] + 1; zmianyB++; } } check = !check; } if (check) { if (tonyB[n - 1] >= tonyB[n - 1 - 1]) { tonyB[n - 1] = tonyB[n - 1 - 1] - 1; zmianyB++; } } else { if (tonyB[n - 1] <= tonyB[n - 1 - 1]) { tonyB[n - 1] = tonyB[n - 1 - 1] + 1; zmianyB++; } } if (zmianyA > zmianyB) cout << zmianyB; else cout << zmianyA; } |