Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- const int maxn = 2002;
- const int INF = 1e9;
- vector<pair<int, int> > graph[maxn];
- int n;
- int dp[maxn][4100];
- int rek(int at, int number) {
- if(at >= n) {
- return 0;
- }
- if(dp[at][number] != -1) {
- return dp[at][number];
- }
- int res = INF;
- int idx = (int) (lower_bound(graph[at].begin(), graph[at].end(), make_pair(number, 0)) - graph[at].begin());
- if(idx >= 0 and idx < graph[at].size()) {
- for(int i = idx; i < (int) graph[at].size(); i++) {
- int nxt_number = graph[at][i].first;
- int swaps = graph[at][i].second;
- res = min(res, rek(at + 1, nxt_number) + swaps);
- }
- }
- return dp[at][number] = res;
- }
- int main()
- {
- cin >> n;
- vector<int> v(n);
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- for(int i = 0; i < n; i++) {
- for(int j = 0; j <= 4095; j++) {
- dp[i][j] = -1;
- if(__builtin_popcount(j) == __builtin_popcount(v[i])) {
- int swaps = __builtin_popcount(v[i] ^ j) / 2;
- graph[i].push_back(make_pair(j, swaps));
- }
- }
- }
- cout << rek(0, 0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement