#include <iostream> #include <vector> #include <algorithm> using namespace std; int checkB(vector<int> & d, bool nextUp) { int ret = 0; int cr = d[0]; for(int i=1;i<d.size();i++) { if(nextUp){ if(d[i]<=cr){ ret+=1; cr = 1000000; } else cr = d[i]; nextUp = false; } else{ if(d[i]>=cr){ ret+=1; cr = -1000000; } else cr = d[i]; nextUp = true; } } return ret; } void B(){ int n; cin>>n; vector<int> d(n,0); for(int i=0;i<n;i++) cin>>d[i]; int ret = 1000000; int l1 = min(ret, checkB(d,true)); int l2 = min(ret, checkB(d, false)); reverse(d.begin(),d.end()); int l3 = min(ret, checkB(d,true)); int l4 = min(ret, checkB(d, false)); // cout<<l1<<" "<<l2<<" "<<l3<<" "<<l4<<endl; ret = min(ret,l1); ret = min(ret,l2); ret = min(ret,l3); ret = min(ret,l4); cout<<ret<<endl; } int main() { B(); 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int checkB(vector<int> & d, bool nextUp) { int ret = 0; int cr = d[0]; for(int i=1;i<d.size();i++) { if(nextUp){ if(d[i]<=cr){ ret+=1; cr = 1000000; } else cr = d[i]; nextUp = false; } else{ if(d[i]>=cr){ ret+=1; cr = -1000000; } else cr = d[i]; nextUp = true; } } return ret; } void B(){ int n; cin>>n; vector<int> d(n,0); for(int i=0;i<n;i++) cin>>d[i]; int ret = 1000000; int l1 = min(ret, checkB(d,true)); int l2 = min(ret, checkB(d, false)); reverse(d.begin(),d.end()); int l3 = min(ret, checkB(d,true)); int l4 = min(ret, checkB(d, false)); // cout<<l1<<" "<<l2<<" "<<l3<<" "<<l4<<endl; ret = min(ret,l1); ret = min(ret,l2); ret = min(ret,l3); ret = min(ret,l4); cout<<ret<<endl; } int main() { B(); return 0; } |