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