#include <iostream>
using namespace std;
int n, tmp;
int results_lf[50000];
int results_hf[50000];
int change_lf=0;
int change_hf=0;
int min_value=-2000000;
int max_value=2000000;
int main(){
cin >> n;
for(int i=0; i<n; i++){
cin >> tmp;
results_lf[i] = tmp;
results_hf[i] = tmp;
}
// Assuming that the pattern is lower-higher-lower-...
for(int i=1; i<n; i++){
if((i%2==1) and (results_lf[i-1]>=results_lf[i])){
results_lf[i] = max_value;
change_lf++;
}
if((i%2==0) and (results_lf[i-1]<=results_lf[i])){
results_lf[i] = min_value;
change_lf++;
}
}
// Assuming that the pattern is higher-lower-higher...
for(int i=1; i<n; i++){
if((i%2==0) and (results_hf[i-1]>=results_hf[i])){
results_hf[i] = max_value;
change_hf++;
}
if((i%2==1) and (results_hf[i-1]<=results_hf[i])){
results_hf[i] = min_value;
change_hf++;
}
}
int result = min(change_hf, change_lf);
cout << result << endl;
}
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 | #include <iostream> using namespace std; int n, tmp; int results_lf[50000]; int results_hf[50000]; int change_lf=0; int change_hf=0; int min_value=-2000000; int max_value=2000000; int main(){ cin >> n; for(int i=0; i<n; i++){ cin >> tmp; results_lf[i] = tmp; results_hf[i] = tmp; } // Assuming that the pattern is lower-higher-lower-... for(int i=1; i<n; i++){ if((i%2==1) and (results_lf[i-1]>=results_lf[i])){ results_lf[i] = max_value; change_lf++; } if((i%2==0) and (results_lf[i-1]<=results_lf[i])){ results_lf[i] = min_value; change_lf++; } } // Assuming that the pattern is higher-lower-higher... for(int i=1; i<n; i++){ if((i%2==0) and (results_hf[i-1]>=results_hf[i])){ results_hf[i] = max_value; change_hf++; } if((i%2==1) and (results_hf[i-1]<=results_hf[i])){ results_hf[i] = min_value; change_hf++; } } int result = min(change_hf, change_lf); cout << result << endl; } |
English