Advertisement
Josif_tepe

Untitled

Jun 23rd, 2025
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #include <vector>
  4. using namespace std;
  5. const int maxn = 30005;
  6. const int INF = 2e9;
  7. int n;
  8. int A[maxn];
  9. int dp[maxn][4];
  10. int rec(int at, int medal) {
  11.     if(at == n) {
  12.         return 0;
  13.     }
  14.     if(dp[at][medal] != -1) {
  15.         return dp[at][medal];
  16.     }
  17.     int res = INF;
  18.     for(int current_medal = medal; current_medal <= 3; current_medal++) {
  19.         int cost = 0;
  20.         if(A[at] != medal) {
  21.             cost = 1;
  22.         }
  23.        
  24.         for(int next_medal = medal; next_medal <= 3; next_medal++) {
  25.             res = min(res, rec(at + 1, next_medal) + cost);
  26.         }
  27.     }
  28.     dp[at][medal] = res;
  29.     return res;
  30. }
  31. int main() {
  32.     ios_base::sync_with_stdio(false);
  33.     cin >> n;
  34.    
  35.     for(int i = 0; i < n; i++) {
  36.         cin >> A[i];
  37.     }
  38.    
  39.     int res = INF;
  40.     memset(dp, -1, sizeof dp);
  41.     for(int i = 1; i <= 3; i++) {
  42.         res = min(res, rec(0, i));
  43.     }
  44.     reverse(A, A + n);
  45.     memset(dp, -1, sizeof dp);
  46.     for(int i = 1; i <= 3; i++) {
  47.         res = min(res, rec(0, i));
  48.     }
  49.     cout << res << endl;
  50.     return 0;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement