fork download
  1. #include <bits/stdc++.h>
  2. // #include <ext/pb_ds/assoc_container.hpp>
  3. // #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. // using namespace __gnu_pbds;
  6. // typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
  7. #define ll long long
  8. #define ull unsigned long long
  9. #define int long long
  10.  
  11. int dp[5005][3] , a[505] , n;
  12. int rec(int idx , int prev ) {
  13. if (idx == 0 ) {
  14. return 0 ;
  15. }
  16. if (idx <0)
  17. return 1e9;
  18. if (dp[idx][prev] != -1) return dp[idx][prev] ;
  19. int ans= rec(idx -1 , prev)+1;
  20. if ((a[idx]==1 || a[idx ]==3) && prev!=1) {
  21. ans = min(ans , rec(idx - 1 , 1));
  22. }
  23. if ((a[idx]==2 || a[idx]==3) && prev!=2) {
  24. ans = min(ans , rec(idx - 1 , 2));
  25. }
  26. return dp[idx][prev] = ans;
  27. }
  28. void solve() {
  29. cin >> n ;
  30. for(int i = 1 ; i <= n ; i++) {
  31. cin >> a[i] ;
  32. }
  33. memset(dp , -1 , sizeof(dp)) ;
  34. cout << rec(n , 0) ;
  35.  
  36. }
  37.  
  38.  
  39. signed main() {
  40. #ifndef ONLINE_JUDGE
  41. freopen("input.txt", "r", stdin);
  42. freopen("output.txt", "w", stdout);
  43. #endif
  44. ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  45. int tc = 1;
  46. // cin >> tc;
  47. while (tc--) {
  48. solve();
  49. };
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Standard output is empty