Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <cstring>
- using namespace std;
- const int maxn = 2005;
- const int INF = 2e9;
- int n, a[maxn];
- vector<pair<int, int>> graph[maxn];
- int dp[maxn][4100];
- int rec(int at, int prev_number) {
- if(at == n) {
- return 0;
- }
- if(dp[at][prev_number] != -1) {
- return dp[at][prev_number];
- }
- int res = INF;
- for(int i = 0; i < (int) graph[at].size(); i++) {
- int number = graph[at][i].first;
- int swaps = graph[at][i].second;
- if(prev_number <= number) {
- res = min(res, rec(at + 1, number) + swaps);
- }
- }
- dp[at][prev_number] = res;
- return res;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- memset(dp, -1, sizeof dp);
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- }
- for(int i = 0; i < n; i++) {
- for(int j = 1; j <= 4096; j++) {
- if(__builtin_popcount(a[i]) == __builtin_popcount(j)) {
- int x = a[i] ^ j;
- graph[i].push_back(make_pair(j, __builtin_popcount(x) / 2));
- }
- }
- }
- cout << rec(0, 0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment