#include <iostream> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; int a[50000]; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> a[i]; } bool big = true; bool small = true; int result_1 = 0; int result_2 = 0; int previous_1 = -1000000; int previous_2 = 1000000; for (int i = 0; i < n; i++) { if (big) { if (previous_1 >= a[i]) { //if previous bigger we gotta be biggest previous_1 = 1000000; result_1 +=1; } else { previous_1 = a[i]; } } else { if (previous_1 <= a[i]) { //if previous smaller we gotta be smallest previous_1 = -1000000; result_1 +=1; } else { previous_1 = a[i]; } } if (small) { if (previous_2 <= a[i]) { //if previous smaller we gotta be smallest previous_2 = -1000000; result_2 +=1; } else { previous_2 = a[i]; } } else { if (previous_2 >= a[i]) { //if previous bigger we gotta be biggest previous_2 = 1000000; result_2 +=1; } else { previous_2 = a[i]; } } // std::cout << previous_2 << std::endl; big = !big; small = !small; } // int result1 = count(n, a, true); // // int result2 = count(n, a, false); // int result = result_1; if (result > result_2) { result = result_2; } std::cout << result; 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 | #include <iostream> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; int a[50000]; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> a[i]; } bool big = true; bool small = true; int result_1 = 0; int result_2 = 0; int previous_1 = -1000000; int previous_2 = 1000000; for (int i = 0; i < n; i++) { if (big) { if (previous_1 >= a[i]) { //if previous bigger we gotta be biggest previous_1 = 1000000; result_1 +=1; } else { previous_1 = a[i]; } } else { if (previous_1 <= a[i]) { //if previous smaller we gotta be smallest previous_1 = -1000000; result_1 +=1; } else { previous_1 = a[i]; } } if (small) { if (previous_2 <= a[i]) { //if previous smaller we gotta be smallest previous_2 = -1000000; result_2 +=1; } else { previous_2 = a[i]; } } else { if (previous_2 >= a[i]) { //if previous bigger we gotta be biggest previous_2 = 1000000; result_2 +=1; } else { previous_2 = a[i]; } } // std::cout << previous_2 << std::endl; big = !big; small = !small; } // int result1 = count(n, a, true); // // int result2 = count(n, a, false); // int result = result_1; if (result > result_2) { result = result_2; } std::cout << result; return 0; } |