Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <algorithm>
- using namespace std;
- int n;
- int arr[2002];
- vector<pair<int, int> > g[2005];
- int memo[2000][5000];
- int dp (int at, int prev_num){
- if(at == n){
- return 0;
- }
- if(memo[at][prev_num] != -1){
- return memo[at][prev_num];
- }
- int res = 2e9;
- for (int i= 0; i < g[at].size() ; i++) {
- if(g[at][i].first >= prev_num){
- res = min(res,dp(at+1,g[at][i].first) + g[at][i].second);
- }
- }
- return memo[at][prev_num] = res ;
- }
- int main() {
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> arr[i];
- for(int j = 0; j < (1 << 12); j++) {
- if(__builtin_popcount(j) == __builtin_popcount(arr[i])) {
- int p = (j ^ arr[i]);
- g[i].push_back(make_pair(j, __builtin_popcount(p) / 2));
- }
- }
- }
- memset(memo,-1,sizeof memo);
- int res = dp(0,0);
- cout << res;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement