Advertisement
Josif_tepe

Untitled

Oct 17th, 2022
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 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.  
  18.  for(int j=0; j<graph[i].size(); j++){
  19.      if(prev_number <= graph[i][j].first) {
  20.          
  21.          result=min(result, dinamicko(i+1, graph[i][j].first) + graph[i][j].second);
  22.              
  23.          
  24.      }
  25.  }
  26.  return dp[i][prev_number]=result;
  27.  }
  28.  
  29. int main()
  30. {
  31.  
  32.  
  33.     cin>>n;
  34.     for(int i=0; i<n; i++){
  35.         int a;
  36.         cin>>a;
  37.         for(int bitmask=0; bitmask<(1<<12); bitmask++){
  38.             if(__builtin_popcount(bitmask)== __builtin_popcount(a)){
  39.             graph[i].push_back(make_pair(bitmask, (__builtin_popcount(a^bitmask))/2));
  40.             }
  41.         }
  42.     }
  43.     memset(dp, -1, sizeof dp);
  44.    
  45.     cout<<dinamicko(0, 0) << endl;
  46.     return 0;
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement