Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstring>
- //#include <bits/stdc++.h>
- using namespace std;
- vector<pair<int, int>>graph[2000];
- int dp[2002][4100];
- int n;
- int dinamicko(int i, int prev_number){
- if(i==n){
- return 0;
- }
- if(dp[i][prev_number]!=-1){
- return dp[i][prev_number];
- }
- int result = 2e9;
- int min_number_of_swaps = 2e9;
- for(int j=0; j<graph[i].size(); j++){
- if(prev_number <= graph[i][j].first) {
- if(graph[i][j].second < min_number_of_swaps) {
- result=min(result, dinamicko(i+1, graph[i][j].first) + graph[i][j].second);
- min_number_of_swaps = graph[i][j].second;
- if(min_number_of_swaps == 0) {
- break;
- }
- }
- }
- }
- return dp[i][prev_number]=result;
- }
- int main()
- {
- cin>>n;
- for(int i=0; i<n; i++){
- int a;
- cin>>a;
- for(int bitmask=0; bitmask<(1<<12); bitmask++){
- if(__builtin_popcount(bitmask)== __builtin_popcount(a)){
- graph[i].push_back(make_pair(bitmask, (__builtin_popcount(a^bitmask))/2));
- }
- }
- }
- memset(dp, -1, sizeof dp);
- cout<<dinamicko(0, 0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement