CF 259div2 D (状态压缩dp)

本文介绍了一种利用状态压缩动态规划方法解决寻找最小化特定表达式的和谐序列问题。通过预处理素数状态,实现记忆化搜索并找到最优序列。
D. Little Pony and Harmony Chest
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.

A sequence of positive integers bi is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony sequence bi which minimizes the following expression:

You are given sequence ai, help Princess Twilight to find the key.

Input

The first line contains an integer n (1 ≤ n ≤ 100) — the number of elements of the sequences a and b. The next line contains n integersa1, a2, ..., an (1 ≤ ai ≤ 30).

Output

Output the key — sequence bi that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.

Sample test(s)
input
5
1 1 1 1 1
output
1 1 1 1 1 
input
5
1 6 4 2 8
output
1 5 3 1 8 

比赛的时候想直接爆搜加剪枝,无限TLE。赛后学了这个状态压缩的姿势。因为b最多选58(否则选1就好了),而58内只有16个素数,可以把它们压成一个状态,先预处理出1~58内所有数对应的状态。这样记忆化搜索的时候,记录当前在选第几个数,和已经使用过的素数的状态,再加上路径还原,就可以得到答案了。
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<stack>
#include<iostream>
#include<queue>
#include<cmath>
#include<string>
#include<set>
#include<map>
using namespace std;
const int maxn = 100 + 5;
const int mod = 1000000000 + 7;
const double eps = 1e-7;
const int INF = 1000000000;
typedef long long LL;
typedef pair<int, int> P;

int prime[16] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};
int Hash[maxn];
int a[maxn];
int Min, n;
int ans[maxn];
int dp[maxn][1<<16];
P trace[maxn][1<<16];

int dfs(int pos, int use){

    if(dp[pos][use] != -1)
        return dp[pos][use];
    if(pos == n)
        return dp[pos][use] = 0;
    int Min = INF, chose;
    for(int i = 1;i < 59;i++){
        if((Hash[i]&use) == 0){
            int tem = dfs(pos+1, use|Hash[i])+abs(a[pos]-i);
            if(tem < Min){
                Min = tem;
                chose = i;
            }
        }
    }
    trace[pos][use] = P(use|Hash[chose], chose);
    return dp[pos][use] = Min;
}

int main(){

    for(int i = 1;i < 59;i++){
        Hash[i] = 0;
        for(int j = 0;j < 16;j++){
            if(i%prime[j]){
                Hash[i] = Hash[i]*2;
            }
            else{
                Hash[i] = Hash[i]*2+1;
            }
        }
    }

    while(cin >> n){
        for(int i = 0;i < n;i++){
            cin >> a[i];
        }
        memset(dp, -1, sizeof dp);
        dfs(0, 0);

        int pos = 0, use = 0;
        while(1){
            if(pos == n)
                break;
            ans[pos] = trace[pos][use].second;
            use = trace[pos][use].first;
            pos++;
        }
        for(int i = 0;i < n;i++)
            cout << ans[i] << ' ';
        cout << endl;
    }
    return 0;
}


六、状压DP的优化技巧 6.1 预处理合法状态 很多问中,大部分状态是不合法的,可以预先筛选: cpp vector valid_states; for (int state = 0; state < (1 << n); ++state) { if (check(state)) { // 检查state是否合法 valid_states.push_back(state); } } 6.2 滚动数组优化 当状态只依赖前一个阶段时,可以节省空间: cpp vector<vector> dp(2, vector(size)); // 只保留当前和上一个状态 int now = 0, prev = 1; for (int i = 1; i <= n; ++i) { swap(now, prev); for (auto& state : valid_states) { dp[now][state] = 0; // 清空当前状态 // 状态转移… } } 6.3 记忆化搜索实现 有时递归形式更直观: cpp int memo[1<<20][20]; // 记忆化数组 int dfs(int state, int u) { if (memo[state][u] != -1) return memo[state][u]; // 递归处理… return memo[state][u] = res; } 七、常见问与调试技巧 7.1 常见错误 位运算优先级:总是加括号,如(state & (1 << i)) 数组越界:状态数是2ⁿ,不是n 初始状态设置错误:比如TSP中dp[1][0] = 0 边界条件处理不当:如全选状态是(1<<n)-1,不是1<<n 7.2 调试建议 打印中间状态:将二进制状态转换为可视化的形式 cpp void printState(int state, int n) { for (int i = n-1; i >= 0; --i) cout << ((state >> i) & 1); cout << endl; } 从小规模测试用例开始(如n=3,4) 使用assert检查关键假设 八、学习路线建议 初级阶段: 练习基本位操作 解决简单状压问(如LeetCode 464、526) 中级阶段: 掌握经典模型(TSP、棋盘覆盖) 学习优化技巧(预处理、滚动数组) 高级阶段: 处理高维状压(如需要同时压缩多个状态) 结合其他算法(如BFS、双指针) 九、实战练习目推荐 入门: LeetCode 78. Subsets(理解状态表示) LeetCode 464. Can I Win(简单状压DP) 中等: LeetCode 526. Beautiful Arrangement LeetCode 691. Stickers to Spell Word 经典: POJ 2411. Mondriaan’s Dream(棋盘覆盖) HDU 3001. Travelling(三进制状压) 挑战: Codeforces 8C. Looking for Order Topcoder SRM 556 Div1 1000. LeftRightDigitsGame2 记住,掌握状压DP的关键在于: 彻底理解二进制状态表示 熟练运用位运算 通过大量练习培养直觉 希望这份超详细的教程能帮助你彻底掌握状压DP!如果还有任何不明白的地方,可以针对具体问继续深入探讨。 请帮我转成markdown语法输出,谢谢
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值