#include <iostream> #include <cstdio> using namespace std; int main() { int tmp; bool last_forward = true; bool last_backward = true; int zmiany_forward = 0; int zmiany_backward = 0; int n; string in = ""; scanf("%i", &n); int tab_forward[n]; int tab_backward[n]; for(int i = 0; i<n;i++){ scanf("%i", &tmp); tab_forward[i] = tmp; tab_backward[n-i-1] = tmp; } for(int i = 1; i<n-1;i++){ if(tab_forward[i-1] > tab_forward[i] && tab_forward[i] < tab_forward[i+1]){ last_forward = true; } else if(tab_forward[i-1] < tab_forward[i] && tab_forward[i] > tab_forward[i+1]){ last_forward = false; } else if(tab_forward[i-1] > tab_forward[i] && tab_forward[i] > tab_forward[i+1]){ tab_forward[i+1] = 1000001; zmiany_forward++; last_forward = true; } else if(tab_forward[i-1] < tab_forward[i] && tab_forward[i] < tab_forward[i+1]){ tab_forward[i+1] = -1000001; zmiany_forward++; last_forward = false; } else if(tab_forward[i-1] > tab_forward[i] && tab_forward[i] == tab_forward[i+1]){ tab_forward[i+1] = 1000001; zmiany_forward++; last_forward = true; } else if(tab_forward[i-1] < tab_forward[i] && tab_forward[i] == tab_forward[i+1]){ tab_forward[i+1] = -1000001; zmiany_forward++; last_forward = false; } else if(tab_forward[i-1] == tab_forward[i] && tab_forward[i] == tab_forward[i+1]){ if(last_forward){ tab_forward[i] = -1000001; } else{ tab_forward[i] = 1000001; } zmiany_forward++; } if(tab_backward[i-1] > tab_backward[i] && tab_backward[i] < tab_backward[i+1]){ last_backward = true; } else if(tab_backward[i-1] < tab_backward[i] && tab_backward[i] > tab_backward[i+1]){ last_backward = false; } else if(tab_backward[i-1] > tab_backward[i] && tab_backward[i] > tab_backward[i+1]){ tab_backward[i+1] = 1000001; zmiany_backward++; last_backward = true; } else if(tab_backward[i-1] < tab_backward[i] && tab_backward[i] < tab_backward[i+1]){ tab_backward[i+1] = -1000001; zmiany_backward++; last_backward = false; } else if(tab_backward[i-1] > tab_backward[i] && tab_backward[i] == tab_backward[i+1]){ tab_backward[i+1] = 1000001; zmiany_backward++; last_backward = true; } else if(tab_backward[i-1] < tab_backward[i] && tab_backward[i] == tab_backward[i+1]){ tab_backward[i+1] = -1000001; zmiany_backward++; last_backward = false; } else if(tab_backward[i-1] == tab_backward[i] && tab_backward[i] == tab_backward[i+1]){ if(last_backward){ tab_backward[i] = -1000001; } else{ tab_backward[i] = 1000001; } zmiany_backward++; } } if(zmiany_backward < zmiany_forward) { zmiany_forward = zmiany_backward; } printf("%i", zmiany_forward); 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #include <iostream> #include <cstdio> using namespace std; int main() { int tmp; bool last_forward = true; bool last_backward = true; int zmiany_forward = 0; int zmiany_backward = 0; int n; string in = ""; scanf("%i", &n); int tab_forward[n]; int tab_backward[n]; for(int i = 0; i<n;i++){ scanf("%i", &tmp); tab_forward[i] = tmp; tab_backward[n-i-1] = tmp; } for(int i = 1; i<n-1;i++){ if(tab_forward[i-1] > tab_forward[i] && tab_forward[i] < tab_forward[i+1]){ last_forward = true; } else if(tab_forward[i-1] < tab_forward[i] && tab_forward[i] > tab_forward[i+1]){ last_forward = false; } else if(tab_forward[i-1] > tab_forward[i] && tab_forward[i] > tab_forward[i+1]){ tab_forward[i+1] = 1000001; zmiany_forward++; last_forward = true; } else if(tab_forward[i-1] < tab_forward[i] && tab_forward[i] < tab_forward[i+1]){ tab_forward[i+1] = -1000001; zmiany_forward++; last_forward = false; } else if(tab_forward[i-1] > tab_forward[i] && tab_forward[i] == tab_forward[i+1]){ tab_forward[i+1] = 1000001; zmiany_forward++; last_forward = true; } else if(tab_forward[i-1] < tab_forward[i] && tab_forward[i] == tab_forward[i+1]){ tab_forward[i+1] = -1000001; zmiany_forward++; last_forward = false; } else if(tab_forward[i-1] == tab_forward[i] && tab_forward[i] == tab_forward[i+1]){ if(last_forward){ tab_forward[i] = -1000001; } else{ tab_forward[i] = 1000001; } zmiany_forward++; } if(tab_backward[i-1] > tab_backward[i] && tab_backward[i] < tab_backward[i+1]){ last_backward = true; } else if(tab_backward[i-1] < tab_backward[i] && tab_backward[i] > tab_backward[i+1]){ last_backward = false; } else if(tab_backward[i-1] > tab_backward[i] && tab_backward[i] > tab_backward[i+1]){ tab_backward[i+1] = 1000001; zmiany_backward++; last_backward = true; } else if(tab_backward[i-1] < tab_backward[i] && tab_backward[i] < tab_backward[i+1]){ tab_backward[i+1] = -1000001; zmiany_backward++; last_backward = false; } else if(tab_backward[i-1] > tab_backward[i] && tab_backward[i] == tab_backward[i+1]){ tab_backward[i+1] = 1000001; zmiany_backward++; last_backward = true; } else if(tab_backward[i-1] < tab_backward[i] && tab_backward[i] == tab_backward[i+1]){ tab_backward[i+1] = -1000001; zmiany_backward++; last_backward = false; } else if(tab_backward[i-1] == tab_backward[i] && tab_backward[i] == tab_backward[i+1]){ if(last_backward){ tab_backward[i] = -1000001; } else{ tab_backward[i] = 1000001; } zmiany_backward++; } } if(zmiany_backward < zmiany_forward) { zmiany_forward = zmiany_backward; } printf("%i", zmiany_forward); return 0; } |