#include <bits/stdc++.h> using namespace std; int n, tone[50004], ans=50004; set <int> S; int peakL[50004], peakR[50004], valleyL[50004], valleyR[50004]; int main() {ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i=1; i<=n; i++){ cin >> tone[i]; S.insert(tone[i]); } for (int i=2; i<=n; i++){ if (tone[i]>tone[i-1] && (tone[i-1]<tone[i-2] || i==2)) peakL[i]=peakL[i-2]; else peakL[i]=peakL[i-2]+1; if (tone[i]<tone[i-1] && (tone[i-1]>tone[i-2] || i==2)) valleyL[i]=valleyL[i-2]; else valleyL[i]=valleyL[i-2]+1; } for (int i=n-1; i>0; i--){ if (tone[i]>tone[i+1] && (tone[i+1]<tone[i+2] || i==n-1)) peakR[i]=peakR[i+2]; else peakR[i]=peakR[i+2]+1; if (tone[i]<tone[i+1] && (tone[i-1]>tone[i-2] || i==n-1)) valleyR[i]=valleyR[i+2]; else valleyR[i]=valleyR[i+2]+1; } for (int i=1; i<=n; i++) //cout <<i<<": peak="<<peakL[i]+peakR[i]<<" valley="<<valleyL[i]+valleyR[i]<<endl; ans=min(ans, min(peakL[i]+peakR[i], valleyL[i]+valleyR[i])); cout <<ans; } /* 5 1 4 1 3 3 1 4 2 1 1 1 1 6 1 2 1 3 7 6 9 9 2 4 2 0 2 1 3 7 6 9 */
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; int n, tone[50004], ans=50004; set <int> S; int peakL[50004], peakR[50004], valleyL[50004], valleyR[50004]; int main() {ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i=1; i<=n; i++){ cin >> tone[i]; S.insert(tone[i]); } for (int i=2; i<=n; i++){ if (tone[i]>tone[i-1] && (tone[i-1]<tone[i-2] || i==2)) peakL[i]=peakL[i-2]; else peakL[i]=peakL[i-2]+1; if (tone[i]<tone[i-1] && (tone[i-1]>tone[i-2] || i==2)) valleyL[i]=valleyL[i-2]; else valleyL[i]=valleyL[i-2]+1; } for (int i=n-1; i>0; i--){ if (tone[i]>tone[i+1] && (tone[i+1]<tone[i+2] || i==n-1)) peakR[i]=peakR[i+2]; else peakR[i]=peakR[i+2]+1; if (tone[i]<tone[i+1] && (tone[i-1]>tone[i-2] || i==n-1)) valleyR[i]=valleyR[i+2]; else valleyR[i]=valleyR[i+2]+1; } for (int i=1; i<=n; i++) //cout <<i<<": peak="<<peakL[i]+peakR[i]<<" valley="<<valleyL[i]+valleyR[i]<<endl; ans=min(ans, min(peakL[i]+peakR[i], valleyL[i]+valleyR[i])); cout <<ans; } /* 5 1 4 1 3 3 1 4 2 1 1 1 1 6 1 2 1 3 7 6 9 9 2 4 2 0 2 1 3 7 6 9 */ |