Advertisement
Josif_tepe

Untitled

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