Advertisement
Josif_tepe

Untitled

Oct 30th, 2022
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. using namespace std;
  5. vector<pair<int, int> > g[2005];
  6. int n;
  7. int dp[2005][4100];
  8. int rec(int at, int prev_number) {
  9.     if(at == n) {
  10.         return 0;
  11.     }
  12.     if(dp[at][prev_number] != -1) {
  13.         return dp[at][prev_number];
  14.     }
  15.     int result = 2e9;
  16.     for(int i = 0; i < (int) g[at].size(); i++) {
  17.         int number = g[at][i].first;
  18.         if(prev_number <= number) {
  19.             result = min(result, rec(at + 1, number) + g[at][i].second);
  20.         }
  21.     }
  22.     return dp[at][prev_number] = result;
  23. }
  24. int main()
  25. {
  26.     ios_base::sync_with_stdio(false);
  27.     cin >> n;
  28.     memset(dp, -1, sizeof dp);
  29.     for(int i = 0; i < n; i++) {
  30.         int x;
  31.         cin >> x;
  32.        
  33.         for(int number = 0; number < (1 << 12); number++) {
  34.             if(__builtin_popcount(number) == __builtin_popcount(x)) {
  35.                 g[i].push_back(make_pair(number, __builtin_popcount(number ^ x) / 2));
  36.                
  37.             }
  38.         }
  39.     }
  40.     cout << rec(0, 0) << endl;
  41.     return 0;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement