#include <cstdio> int main() { int n; scanf("%d", &n); int* rec = new int[n + 1]; for (int i = 0; i < n; ++i) { scanf("%i", &rec[i]); } int min = 0; int max = 0; int last_idx_min = 0; int last_idx_max = 0; for (int i = 1; i < n - 1; ++i) { if (rec[i - 1] < rec[i] && rec[i] > rec[i + 1]) { if (i % 2 == 0) { if (((i - last_idx_max) % 3 == 0 || last_idx_max == 0) && ((i - last_idx_max) <= 2 || last_idx_max == 0 || (i - last_idx_max) % 2 == 0)) { max+=3; last_idx_max = i; } else if ((i-last_idx_max)%3 != 0) { max+=(i-last_idx_max)%3; last_idx_max = i; }else { last_idx_max = 0; } } else { if (((i - last_idx_min) % 3 == 0 || last_idx_min == 0) && ((i - last_idx_min) <= 2 || last_idx_min == 0 || (i - last_idx_min) % 2 == 0)) { min+=3; last_idx_min = i; } else if ((i-last_idx_min)%3 != 0) { min+=(i-last_idx_min)%3; }else { last_idx_min = 0; } } } else if (rec[i - 1] > rec[i] && rec[i] < rec[i + 1]) { if (i % 2 == 0) { if (((i - last_idx_min) % 3 == 0 || last_idx_min == 0) && ((i - last_idx_min) <= 2 || last_idx_min == 0 || (i - last_idx_min) % 2 == 0)) { min+=3; last_idx_min = i; } else if ((i-last_idx_min)%3 != 0) { min+=(i-last_idx_min)%3; last_idx_min = i; } else { last_idx_min = 0; } } else { if (((i - last_idx_max) % 3 == 0 || last_idx_max == 0) && ((i - last_idx_max) <= 2 || last_idx_max == 0 || (i - last_idx_max) % 2 == 0)) { max+=3; last_idx_max = i; last_idx_min = 0; } else if ((i-last_idx_max)%3 != 0) { max+=(i-last_idx_max)%3; last_idx_max = i; }else { last_idx_max = 0; } } } } if (max > min) { printf("%d", ((n - max) / 2) + (n - max) % 2); } else { printf("%d", ((n - min) / 2)); } 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 | #include <cstdio> int main() { int n; scanf("%d", &n); int* rec = new int[n + 1]; for (int i = 0; i < n; ++i) { scanf("%i", &rec[i]); } int min = 0; int max = 0; int last_idx_min = 0; int last_idx_max = 0; for (int i = 1; i < n - 1; ++i) { if (rec[i - 1] < rec[i] && rec[i] > rec[i + 1]) { if (i % 2 == 0) { if (((i - last_idx_max) % 3 == 0 || last_idx_max == 0) && ((i - last_idx_max) <= 2 || last_idx_max == 0 || (i - last_idx_max) % 2 == 0)) { max+=3; last_idx_max = i; } else if ((i-last_idx_max)%3 != 0) { max+=(i-last_idx_max)%3; last_idx_max = i; }else { last_idx_max = 0; } } else { if (((i - last_idx_min) % 3 == 0 || last_idx_min == 0) && ((i - last_idx_min) <= 2 || last_idx_min == 0 || (i - last_idx_min) % 2 == 0)) { min+=3; last_idx_min = i; } else if ((i-last_idx_min)%3 != 0) { min+=(i-last_idx_min)%3; }else { last_idx_min = 0; } } } else if (rec[i - 1] > rec[i] && rec[i] < rec[i + 1]) { if (i % 2 == 0) { if (((i - last_idx_min) % 3 == 0 || last_idx_min == 0) && ((i - last_idx_min) <= 2 || last_idx_min == 0 || (i - last_idx_min) % 2 == 0)) { min+=3; last_idx_min = i; } else if ((i-last_idx_min)%3 != 0) { min+=(i-last_idx_min)%3; last_idx_min = i; } else { last_idx_min = 0; } } else { if (((i - last_idx_max) % 3 == 0 || last_idx_max == 0) && ((i - last_idx_max) <= 2 || last_idx_max == 0 || (i - last_idx_max) % 2 == 0)) { max+=3; last_idx_max = i; last_idx_min = 0; } else if ((i-last_idx_max)%3 != 0) { max+=(i-last_idx_max)%3; last_idx_max = i; }else { last_idx_max = 0; } } } } if (max > min) { printf("%d", ((n - max) / 2) + (n - max) % 2); } else { printf("%d", ((n - min) / 2)); } return 0; } |