Codeforces 454D - Little Pony and Harmony Chest
454D - Little Pony and Harmony Chest
思路:
状压dp,由于1的时候肯定满足题意,而ai最大是30,所以只要大于等于59都可以用1替换,所以答案在1到59之间
然后筛出1到58之间的质数,只有16个,把1到58的数的状态由这16个质数表示,如果整除这个质数则二进制中这一位为1,否则则为0
状态:dp[i][j]表示到第i个数为止选取的数的状态为j的最小差和
初始状态:dp[0][0]=0
状态转移:
dp[i+1][j|sta[k]]=min(dp[i+1][j|sta[k]],dp[i][j]+abs(k-a[i+1]))(j&sta[k]==0,1<=k<=58)
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int INF=0x3f3f3f3f;
int sta[];
int a[];
int res[];
int dp[][(<<)+];
int ans[][(<<)+];
int prime[]={,,,,,,,,,,,,,,,};
int getsta(int x){
int ans=;
for(int i=;i<;i++){
if(x%prime[i]==)ans|=<<i;
}
return ans;
}
void dfs(int n,int s){
if(n==)return ;
res[n]=ans[n][s];
dfs(n-,s^sta[res[n]]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
for(int i=;i<;i++){
sta[i]=getsta(i);
}
int n;
cin>>n;
for(int i=;i<=n;i++)cin>>a[i];
mem(dp,INF);
mem(ans,-);
dp[][]=ans[][]=;
for(int i=;i<n;i++){
for(int j=;j<(<<);j++){
if(ans[i][j]==-)continue;
for(int k=;k<;k++){
if(j&sta[k])continue;
int s=j|sta[k];
if(ans[i+][s]==-||dp[i][j]+abs(k-a[i+])<dp[i+][s]){
dp[i+][s]=dp[i][j]+abs(k-a[i+]);
ans[i+][s]=k;
}
}
}
}
int mn=INF,s=;
for(int i=;i<(<<);i++){
if(dp[n][i]<mn){
mn=dp[n][i];
s=i;
}
}
dfs(n,s);
for(int i=;i<=n;i++)cout<<res[i]<<" ";
cout<<endl;
return ;
}
Codeforces 454D - Little Pony and Harmony Chest的更多相关文章
- Codeforces 453B Little Pony and Harmony Chest:状压dp【记录转移路径】
题目链接:http://codeforces.com/problemset/problem/453/B 题意: 给你一个长度为n的数列a,让你构造一个长度为n的数列b. 在保证b中任意两数gcd都为1 ...
- Codeforces 4538 (状态压缩dp)Little Pony and Harmony Chest
Little Pony and Harmony Chest 经典状态压缩dp #include <cstdio> #include <cstring> #include < ...
- 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 ...
- CF453B Little Pony and Harmony Chest (状压DP)
CF453B CF454D Codeforces Round #259 (Div. 2) D Codeforces Round #259 (Div. 1) B D. Little Pony and H ...
- [CF453B]Little Pony and Harmony Chest
[CF453B]Little Pony and Harmony Chest 题目大意: 给你一个长度为\(n(n\le100)\)的正整数序列\(A(A_i\le30)\),求一个正整数序列\(B\) ...
- CF 435B Little Pony and Harmony Chest
Little Pony and Harmony Chest 题解: 因为 1 <= ai <= 30 所以 1 <= bi <= 58, 因为 59 和 1 等效, 所以不需 ...
- M - Little Pony and Harmony Chest 状压dp
M - Little Pony and Harmony Chest 怎么感觉自己越来越傻了,都知道状态的定义了还没有推出转移方程. 首先这个a的范围是0~30 这里可以推出 b数组的范围 0~60 ...
- codeforces 454 D. Little Pony and Harmony Chest(状压dp)
题目链接:http://codeforces.com/contest/454/problem/D 题意:给定一个序列a, 求一序列b,要求∑|ai−bi|最小.并且b中任意两数的最大公约数为1. 题解 ...
- Codeforces Round #259 (Div. 2)-D. Little Pony and Harmony Chest
题目范围给的很小,所以有状压的方向. 我们是构造出一个数列,且数列中每两个数的最大公约数为1; 给的A[I]<=30,这是一个突破点. 可以发现B[I]中的数不会很大,要不然就不满足,所以B[I ...
随机推荐
- html03
快速开发指南:1.新建页面之后,编写外部样式表文件,引入到HTML页面中2.用浏览器打开页面->F12->sources->打开css文件 右边编写样式,左边查看效果3.样式编写完成 ...
- Microsoft Interview第一轮
上来随意交谈了一小会儿,开了点小玩笑,chat了一些关于他们recruter行程的话题,缓和了一下气氛. 进入正题,问了做的research的方向,我说是DLT,然后大概给他讲解了一下具体是什么, 跟 ...
- python解决matplotlib中文坐标值乱码的问题
加上这句话即可 plt.rcParams['font.sans-serif']=['SimHei'] 效果:
- 引入jQuery的src设置
1.引用在线jQuery 谷歌.微软.百度都有提供在线的jQuery引用.比如 <script src="http://code.jquery.com/jquery-latest.js ...
- Hive sql和Presto sql的一些对比
最近由于工作上和生活上的一些事儿好久没来博客园了,但是写博客的习惯还是得坚持,新的一年需要更加努力,困知勉行,终身学习,每天都保持空杯心态.废话不说,写一些最近使用到的Presto SQL和Hive ...
- Adobe Illustrator CS6 界面文字按钮太小,高分屏win10PS/AI等软件界面字太小解决方法
Adobe Illustrator CS6 界面文字按钮太小,高分屏win10PS/AI等软件界面字太小解决方法 Adobe App Scaling on High DPI Displays (FIX ...
- ThinkPHP CURD mysql操作
ThinkPHP CURD操作 ThinkPHP提供了灵活和方便的数据操作方法,对数据库操作的四个基本操作(CURD):创建.更新.读取和删除的实现是最基本的,也是必须掌握的,在这基础之上才能熟悉更多 ...
- jquery基础框架
(function(window,undefined){ var arr = [], push = arr.push, slice = arr.slice; //首先要做的就是封装一个parseHtm ...
- Finalize和Dispose的区别
https://www.cnblogs.com/Jessy/articles/2552839.html
- 多态时最好将基类的析构函数设为virtual、 C++中两个类相互包含引用问题 (转载)
多态:http://blog.csdn.net/tmljs1988/article/details/8146521 C++中两个类相互包含引用问题:http://blog.csdn.net/leo11 ...