Codeforces 453B Little Pony and Harmony Chest:状压dp【记录转移路径】
题目链接: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【记录转移路径】的更多相关文章
- 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 ...
- M - Little Pony and Harmony Chest 状压dp
M - Little Pony and Harmony Chest 怎么感觉自己越来越傻了,都知道状态的定义了还没有推出转移方程. 首先这个a的范围是0~30 这里可以推出 b数组的范围 0~60 ...
- Codeforces 454D - Little Pony and Harmony Chest
454D - Little Pony and Harmony Chest 思路: 状压dp,由于1的时候肯定满足题意,而ai最大是30,所以只要大于等于59都可以用1替换,所以答案在1到59之间 然后 ...
- 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 ...
- Educational Codeforces Round 13 E. Another Sith Tournament 状压dp
E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...
- Codeforces 1225G - To Make 1(bitset+状压 dp+找性质)
Codeforces 题目传送门 & 洛谷题目传送门 还是做题做太少了啊--碰到这种题一点感觉都没有-- 首先我们来证明一件事情,那就是存在一种合并方式 \(\Leftrightarrow\) ...
- 【状压dp】Hamiton路径
描述 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamilton路径. Hamilton路径的定义是从 0 到 n-1 不重不漏地经过每个点 ...
- CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP
题目传送门 https://codeforces.com/contest/1103/problem/D 题解 失去信仰的低水平选手的看题解的心路历程. 一开始看题目以为是选出一些数,每个数可以除掉一个 ...
- Codeforces 279D The Minimum Number of Variables 状压dp
The Minimum Number of Variables 我们定义dp[ i ][ mask ]表示是否存在 处理完前 i 个a, b中存者 a存在的状态是mask 的情况. 然后用sosdp处 ...
随机推荐
- listView的异步加载数据
1 public class MainActivity extends Activity { 2 3 private ListView listView; 4 private ArrayList< ...
- Android Studio 使用笔记:查看类结构和继承关系
选中类 ,按下F4,可以打开类的源代码 在 Eclipse 中我们可以使用 Ctrl + O 组合热键查看类的结构,Android Studio 中也可以做到. View -> Tool Win ...
- POJ 1654 area 解题
Description You are going to compute the area of a special kind of polygon. One vertex of the polygo ...
- left join,right join用法简介
方法一(推荐): select a.man_id,man_name,d.sex_name,zw_name,c.money from man as a left join zw as b on a.zw ...
- 为什么要用markdown写作
无论是 EPUB, mobi,还是 Kindle 用的专有格式 .azw,都只是把一堆 `HTML 文件打包`而已.如果你写的是书,用 Markdown 标注格式之后,可以很方便地转为以上格式 使用W ...
- RecyclerView 必知必会(转)
[腾讯Bugly干货分享]RecyclerView 必知必会 本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:http://mp.weixin.qq.com ...
- CLR内存回收机制
代龄机制. 通过递归构建可达对象图,不可达的对象会被回收,然后CLR会矫正对象指针. 对于终止化/Finalize对象, 一开始时这些对象指针/根/引用会被放到终止化链表中,当CLR垃圾收集开始时,那 ...
- Linux5_环境变量
1.总结背景 在linux系统下,下载并安装了应用程序,很有可能在键入它的名称时出现“command not found”的提示内容. 每次都到安装目标文件夹内,找到可执行文件来进行操作就太繁琐了.这 ...
- 自定义cginc文件
首先定义一个cginc文件如下所示: #ifndef MY_CG_INCLUDE #define MY_CG_INCLUDE struct appdata_x { float4 vertex : PO ...
- python基础: day4作业计算器
作业:计算器开发 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - ...