Advertisement
Josif_tepe

Untitled

Oct 17th, 2022
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. //#include <bits/stdc++.h>
  5. using namespace std;
  6.  vector<pair<int, int>>graph[2000];
  7.  int dp[2002][4100];
  8.  int n;
  9.  int dinamicko(int i, int prev_number){
  10.  if(i==n){
  11.     return 0;
  12.  }
  13.  if(dp[i][prev_number]!=-1){
  14.     return dp[i][prev_number];
  15.  }
  16. int result = 2e9;
  17.      int min_number_of_swaps = 2e9;
  18.  for(int j=0; j<graph[i].size(); j++){
  19.      if(prev_number <= graph[i][j].first) {
  20.          if(graph[i][j].second < min_number_of_swaps) {
  21.          result=min(result, dinamicko(i+1, graph[i][j].first) + graph[i][j].second);
  22.              min_number_of_swaps = graph[i][j].second;
  23.              
  24.              if(min_number_of_swaps == 0) {
  25.                  break;
  26.              }
  27.          }
  28.      }
  29.  }
  30.  return dp[i][prev_number]=result;
  31.  }
  32.  
  33. int main()
  34. {
  35.  
  36.  
  37.     cin>>n;
  38.     for(int i=0; i<n; i++){
  39.         int a;
  40.         cin>>a;
  41.         for(int bitmask=0; bitmask<(1<<12); bitmask++){
  42.             if(__builtin_popcount(bitmask)== __builtin_popcount(a)){
  43.             graph[i].push_back(make_pair(bitmask, (__builtin_popcount(a^bitmask))/2));
  44.             }
  45.         }
  46.     }
  47.     memset(dp, -1, sizeof dp);
  48.    
  49.     cout<<dinamicko(0, 0) << endl;
  50.     return 0;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement