#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef string str; typedef unsigned long long ull; #define pb push_back #define mp make_pair const int MAX_N = 50007; int sounds[MAX_N]; int sounds1[MAX_N]; int main(){ ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; for(int i = 0; i < n; ++i) { int tmp; cin >> tmp; sounds[i] = tmp; sounds1[i] = tmp; } int res = 0; bool next_higher = true; for(int i = 1; i < n; ++i) { if(next_higher && sounds[i] <= sounds[i - 1]) { res++; sounds[i] = sounds[i + 1] + 1; } else if(!next_higher && sounds[i] >= sounds[i - 1]) { res++; sounds[i] = sounds[i + 1] - 1; } if(next_higher) next_higher = false; else next_higher = true; } int res1 = 0; next_higher = false; for(int i = 1; i < n; ++i) { if(next_higher && sounds1[i] <= sounds1[i - 1]) { res1++; sounds1[i] = sounds1[i + 1] + 1; } else if(!next_higher && sounds1[i] >= sounds1[i - 1]) { res1++; sounds1[i] = sounds1[i + 1] - 1; } if(next_higher) next_higher = false; else next_higher = true; } cout << min(res,res1); 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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef string str; typedef unsigned long long ull; #define pb push_back #define mp make_pair const int MAX_N = 50007; int sounds[MAX_N]; int sounds1[MAX_N]; int main(){ ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; for(int i = 0; i < n; ++i) { int tmp; cin >> tmp; sounds[i] = tmp; sounds1[i] = tmp; } int res = 0; bool next_higher = true; for(int i = 1; i < n; ++i) { if(next_higher && sounds[i] <= sounds[i - 1]) { res++; sounds[i] = sounds[i + 1] + 1; } else if(!next_higher && sounds[i] >= sounds[i - 1]) { res++; sounds[i] = sounds[i + 1] - 1; } if(next_higher) next_higher = false; else next_higher = true; } int res1 = 0; next_higher = false; for(int i = 1; i < n; ++i) { if(next_higher && sounds1[i] <= sounds1[i - 1]) { res1++; sounds1[i] = sounds1[i + 1] + 1; } else if(!next_higher && sounds1[i] >= sounds1[i - 1]) { res1++; sounds1[i] = sounds1[i + 1] - 1; } if(next_higher) next_higher = false; else next_higher = true; } cout << min(res,res1); return 0; } |