题目链接:http://codeforces.com/problemset/problem/453/B

题意:

  给你一个长度为n的数列a,让你构造一个长度为n的数列b。

  在保证b中任意两数gcd都为1的情况下,使得 ∑|a[i]-b[i]|最小。

  让你输出构造的数列b。

  (1<=n<=100, 1<=a[i]<=30)

题解:

  因为1<=a[i]<=30,所以有1<=b[i]<=60,此时才有可能最优。

  因为b中任意两数gcd为1,所以对于一个质因子p[i]只会在一个b[i]中用到。

  所以先处理出1到60这些数所要用到的质因子,状压存在数组f[i]中,第i位为1表示要用到质因子p[i]。

  另外,这题中59这个质因子是用不到的。

  因为它能构成的60以内的数只有59,然而对于最大的a[i]=30来说,b[i]选59和选1是等效的。

  这样就只剩16个质因子了。否则用17个会被卡时间和空间。

  然后开始状压dp。

  表示状态:

    dp[i][state] = min value

    表示该构造b[i]了,质因子的状态为state,此时原式的最小值。

  如何转移:

    dp[i+1][state|(1<<j)] = min dp[i][state] + |a[i]-j|

    枚举当前b[i]选了j,然后转移。

  边界条件:

    dp[0][0] = 0

    ohters = INF

    改构造b[0]了,此时一个质因子还没用过,原式初始为0。

  找出答案:

    枚举质因子状态state,显然最小的dp[n][state]为答案。

  然而现在只是知道了原式能达到的最小值,并不知道构造出的b数列。

  所以在转移的时候要记录下转移路径。

  新开两个数组:

    sel[i][state]:表示从上一步转移到这一步时,b[i-1]选了哪个数字

    sta[i][state]:若状态(i,state)是由(i-1,pre)转移而来的,则sta[i][state]为pre的值。

  所以每次转移的时候将路径和所选的数记录下来:

    if(dp[i][state]+d < dp[i+1][nex])

    {

      dp[i+1][nex]=dp[i][state]+d;

      sel[i+1][nex]=j;

      sta[i+1][nex]=state;

    }

  然后从最终答案的那一步,一直往前一个状态跳,就能找出构造的b数列了。

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#define MAX_N 105
#define MAX_P 20
#define MAX_D 65
#define MAX_S ((1<<16)+50)
#define INF 1000000000 using namespace std; const int p[]={,,,,,,,,,,,,,,,}; int n;
int a[MAX_N];
int dp[MAX_N][MAX_S];
int sel[MAX_N][MAX_S];
int sta[MAX_N][MAX_S];
int f[MAX_D]; inline int abs(int x)
{
return x> ? x : -x;
} int get_f(int x)
{
int state=;
for(int i=;i<;i++)
{
while(x%p[i]==)
{
x/=p[i];
state|=(<<i);
}
}
return state;
} void cal_f()
{
for(int i=;i<=;i++)
{
f[i]=get_f(i);
}
} void cal_dp()
{
memset(dp,0x3f,sizeof(dp));
dp[][]=;
for(int i=;i<n;i++)
{
for(int state=;state<(<<);state++)
{
if(dp[i][state]<INF)
{
for(int j=;j<=;j++)
{
if(!(state&f[j]))
{
int nex=(state|f[j]);
int d=abs(a[i]-j);
if(dp[i][state]+d<dp[i+][nex])
{
dp[i+][nex]=dp[i][state]+d;
sel[i+][nex]=j;
sta[i+][nex]=state;
}
}
}
}
}
}
int ans=INF;
int now;
for(int state=;state<(<<);state++)
{
if(dp[n][state]<ans)
{
ans=dp[n][state];
now=state;
}
}
stack<int> stk;
for(int i=n;i>=;i--)
{
stk.push(sel[i][now]);
now=sta[i][now];
}
while(!stk.empty())
{
cout<<stk.top()<<" ";
stk.pop();
}
cout<<endl;
} int main()
{
cin>>n;
for(int i=;i<n;i++) cin>>a[i];
cal_f();
cal_dp();
}

Codeforces 453B Little Pony and Harmony Chest:状压dp【记录转移路径】的更多相关文章

  1. Codeforces Round #259 (Div. 2) D. Little Pony and Harmony Chest 状压DP

    D. Little Pony and Harmony Chest   Princess Twilight went to Celestia and Luna's old castle to resea ...

  2. M - Little Pony and Harmony Chest 状压dp

    M - Little Pony and Harmony Chest 怎么感觉自己越来越傻了,都知道状态的定义了还没有推出转移方程. 首先这个a的范围是0~30   这里可以推出 b数组的范围 0~60 ...

  3. Codeforces 454D - Little Pony and Harmony Chest

    454D - Little Pony and Harmony Chest 思路: 状压dp,由于1的时候肯定满足题意,而ai最大是30,所以只要大于等于59都可以用1替换,所以答案在1到59之间 然后 ...

  4. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

  5. Educational Codeforces Round 13 E. Another Sith Tournament 状压dp

    E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...

  6. Codeforces 1225G - To Make 1(bitset+状压 dp+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 还是做题做太少了啊--碰到这种题一点感觉都没有-- 首先我们来证明一件事情,那就是存在一种合并方式 \(\Leftrightarrow\) ...

  7. 【状压dp】Hamiton路径

    描述 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamilton路径. Hamilton路径的定义是从 0 到 n-1 不重不漏地经过每个点 ...

  8. CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP

    题目传送门 https://codeforces.com/contest/1103/problem/D 题解 失去信仰的低水平选手的看题解的心路历程. 一开始看题目以为是选出一些数,每个数可以除掉一个 ...

  9. Codeforces 279D The Minimum Number of Variables 状压dp

    The Minimum Number of Variables 我们定义dp[ i ][ mask ]表示是否存在 处理完前 i 个a, b中存者 a存在的状态是mask 的情况. 然后用sosdp处 ...

随机推荐

  1. ArrayList remove注意事项

    例子1: List<Integer>list=new ArrayList<>(); list.add(1); list.add(2); list.add(2); list.ad ...

  2. 50 years of Computer Architecture: From the Mainframe CPU to the Domain-Specific TPU and the Open RISC-V Instruction Set

    1.1960年代(大型机) IBM发明了具有二进制兼容性的ISA——System/360,可以兼容一系列的8到64位的硬件产品,而不必更换操作系统.这是通过微编程实现的,每个计算机模型都有各自的ISA ...

  3. 2017湘潭赛 A题 Determinant (高斯消元取模)

    链接 http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1260 今年湘潭的A题 题意不难 大意是把n*(n+1)矩阵去掉某一列 ...

  4. Ubuntu 16.04 关闭/打开笔记本触摸板

    由于笔记本触摸板太多灵敏,影响使用,所以禁用掉触摸板. 禁用触摸板命令: sudo rmmod psmouse 启用触摸板命令 sudo modprobe psmouse 注意:启用之后可能会有几秒钟 ...

  5. python reduce & map 习题

    基于廖雪峰教程作业 http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014317 ...

  6. Python读取文件数据

    1题目要求: 文本文件有这些数据,需要的只有其中的5个属性,如下颜色标记 像以下的数据达到75万组: 1product/productId: B0000UIXZ4 2product/title: Ti ...

  7. Linux删除乱码文件的方法

    当文件名为乱码的时候,无法通过键盘输入文件名,所以在终端下就不能直接利用rm,mv等命令管理文件了. 我们可以通过以下几种方法删除linux下的乱码文件.(文件名为乱码) l  方法1 我们知道每个文 ...

  8. Linux 随手记(文件操作)

    新建文件夹 mkdir 文件夹名 新建文件 touch 文件名 重命名 mv 文件名 新文件名 将/a目录移动到/b下,并重命名为c mv /a /b/c 复制文件 cp [选项] 源文件或目录 目标 ...

  9. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  10. F - Monkey Banana Problem

    F - Monkey Banana Problem Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...