#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const int maxn = 1e9; const int MAX_N = 1e6 + 7; vector<int> a(MAX_N); vector<int> b(MAX_N); int main(){ //ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n; cin >> n; rep(i,n){ cin >> a[i]; b[i] = a[i]; } //pierwszy rosnie int cnt1 = 0; for(int i = 1; i < n; i++){ if(i%2 == 1){ if(a[i] <= a[i-1]){ cnt1++; a[i] = maxn; } } else{ if(a[i] >= a[i-1]){ cnt1++; a[i] = -maxn; } } } //pierwszy maleje int cnt2 = 0; for(int i = 1; i < n; i++){ if(i%2 == 0){ if(b[i] <= b[i-1]){ cnt2++; b[i] = maxn; } } else{ if(b[i] >= b[i-1]){ cnt2++; b[i] = -maxn; } } } cout << min(cnt1,cnt2); 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 <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const int maxn = 1e9; const int MAX_N = 1e6 + 7; vector<int> a(MAX_N); vector<int> b(MAX_N); int main(){ //ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n; cin >> n; rep(i,n){ cin >> a[i]; b[i] = a[i]; } //pierwszy rosnie int cnt1 = 0; for(int i = 1; i < n; i++){ if(i%2 == 1){ if(a[i] <= a[i-1]){ cnt1++; a[i] = maxn; } } else{ if(a[i] >= a[i-1]){ cnt1++; a[i] = -maxn; } } } //pierwszy maleje int cnt2 = 0; for(int i = 1; i < n; i++){ if(i%2 == 0){ if(b[i] <= b[i-1]){ cnt2++; b[i] = maxn; } } else{ if(b[i] >= b[i-1]){ cnt2++; b[i] = -maxn; } } } cout << min(cnt1,cnt2); return 0; } |