#include "bits/stdc++.h" // Ignacy Boehlke using namespace std; // XIII LO Szczecin template<class A,class B>ostream&operator<<(ostream&os,pair<A,B>p){return os<<'{'<<p.first<<", "<<p.second<<'}';} template<class T>auto operator<<(ostream&os,T v)->decltype(v.end(),os){os<<'{';int i=0;for(auto e:v)os<<(", ")+2*!i++<<e;return os<<'}';} #ifdef DEBUG #define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define LOG(...)(void)0 #endif #define ssize(x)((int)x.size()) #define FOR(a,b,c)for(int a=(b);a<=(c);a++) #define REP(a,b)FOR(a,0,(b)-1) #define ALL(x)(x).begin(), (x).end() #define fi first #define se second using ll=long long; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<int> in(n); REP(i, n) cin >> in[i]; vector<pair<int, int>> dp(n); dp[0] = {0, 1}; dp[1] = {in[1] >= in[0], 1}; FOR(i, 2, n - 1) { auto& d = dp[i]; if (in[i - 2] < in[i - 1]) { if (in[i - 1] > in[i]) { d.fi = dp[i - 2].fi; d.se = d.fi + 1; } else { d.fi = d.se = dp[i - 2].fi + 1; } } else { if (in[i - 1] > in[i]) { d.fi = dp[i - 2].se; d.se = d.fi + 1; } else { d.fi = dp[i - 2].fi + 1; d.se = dp[i - 2].se + 1; } } } LOG(dp); cout << min({dp[n - 1].fi, dp[n - 2].se, (in[n - 1] > in[n - 2] ? dp[n - 2].fi : INT_MAX)}) << '\n'; }
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 | #include "bits/stdc++.h" // Ignacy Boehlke using namespace std; // XIII LO Szczecin template<class A,class B>ostream&operator<<(ostream&os,pair<A,B>p){return os<<'{'<<p.first<<", "<<p.second<<'}';} template<class T>auto operator<<(ostream&os,T v)->decltype(v.end(),os){os<<'{';int i=0;for(auto e:v)os<<(", ")+2*!i++<<e;return os<<'}';} #ifdef DEBUG #define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define LOG(...)(void)0 #endif #define ssize(x)((int)x.size()) #define FOR(a,b,c)for(int a=(b);a<=(c);a++) #define REP(a,b)FOR(a,0,(b)-1) #define ALL(x)(x).begin(), (x).end() #define fi first #define se second using ll=long long; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<int> in(n); REP(i, n) cin >> in[i]; vector<pair<int, int>> dp(n); dp[0] = {0, 1}; dp[1] = {in[1] >= in[0], 1}; FOR(i, 2, n - 1) { auto& d = dp[i]; if (in[i - 2] < in[i - 1]) { if (in[i - 1] > in[i]) { d.fi = dp[i - 2].fi; d.se = d.fi + 1; } else { d.fi = d.se = dp[i - 2].fi + 1; } } else { if (in[i - 1] > in[i]) { d.fi = dp[i - 2].se; d.se = d.fi + 1; } else { d.fi = dp[i - 2].fi + 1; d.se = dp[i - 2].se + 1; } } } LOG(dp); cout << min({dp[n - 1].fi, dp[n - 2].se, (in[n - 1] > in[n - 2] ? dp[n - 2].fi : INT_MAX)}) << '\n'; } |