「CF453B」Little Pony and Harmony Chest
2014年8月2日4,0270
Princess Twilight went to Celestia and Luna’s old castle to research the chest from the Elements of Harmony.


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 integers a1, 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
1 2 |
5 1 1 1 1 1 |
output
1 |
1 1 1 1 1 |
input
1 2 |
5 1 6 4 2 8 |
output
1 |
1 5 3 1 8 |
题解
一开始以为是相邻互质。。。后来发现过不了样例T T
如果相邻互质直接dp即可
大概这样
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define ll long long #define inf 1000000000 using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,ans,mn=inf,v[100005]; int f[105][45],fa[105][45],q[105]; inline int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } int main() { memset(f,127/3,sizeof(f)); n=read(); for(int i=1;i<=n;i++) v[i]=read(); for(int i=1;i<=40;i++)f[1][i]=abs(v[1]-i); for(int i=2;i<=n;i++) for(int a=1;a<=40;a++) { for(int b=1;b<=40;b++) if(gcd(a,b)==1) { if(f[i-1][b]<f[i][a]) { f[i][a]=f[i-1][b];fa[i][a]=b; } } f[i][a]+=abs(v[i]-a); } for(int i=1;i<=40;i++) if(f[n][i]<mn) { ans=i;mn=f[n][i]; } for(int i=n;i;i--) { q[i]=ans;ans=fa[i][ans]; } for(int i=1;i<=n;i++) printf("%d",q[i]); return 0; } |
两两互质就比较麻烦了
首先1-60的质数有17个,去掉59,因为取59不如取1
那么就剩下16个,状压一下T T,转移枚举使用了那个数字,应该要预处理下每个数字用了哪些质因数吧T T,不然似乎比较虚
复杂度2^16*100*60是三亿多,在cf的机子下并且非常多废状态还是妥妥过的啦
我最大的点似乎只跑一秒左右。。。时限是4秒啊
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define ll long long #define inf 1000000000 using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int bin[20]; int n,cnt,ed,mn=inf,ans=0;; int f[105][65536],fa[105][65536]; int v[105],q[105],a[60]; int p[50]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53}; inline int cal(int x) { int sum=0; for(int i=0;i<16;i++) if(x%p[i]==0)sum+=bin[i]; return sum; } int main() { bin[0]=1;for(int i=1;i<20;i++)bin[i]=bin[i-1]<<1; for(int i=1;i<59;i++)a[i]=cal(i); memset(f,-1,sizeof(f)); n=read(); for(int i=1;i<=n;i++)v[i]=read(); ed=(1<<16)-1; f[0][0]=0; for(int i=0;i<n;i++) for(int j=0;j<=ed;j++) if(f[i][j]!=-1) for(int k=1;k<59;k++) { int to=j+a[k],t=f[i][j]+abs(k-v[i+1]); if((j|a[k])==to&&(t<f[i+1][to]||f[i+1][to]==-1)) { f[i+1][to]=t; fa[i+1][to]=k; } } for(int i=0;i<=ed;i++) if(f[n][i]<mn&&f[n][i]!=-1) { ans=i;mn=f[n][i]; } for(int i=n;i;i--) { q[i]=fa[i][ans];ans-=a[q[i]]; } for(int i=1;i<=n;i++) printf("%d ",q[i]); return 0; } |
Subscribe
分类目录
热门文章
- 17891「BZOJ1207」[HNOI2004] 打鼹鼠
- 15472「BZOJ1009」[HNOI2008] GT考试
- 13092NOIP2005过河(青蛙过河)
- 12269NOIP1999拦截导弹
- 10680「BZOJ1023」[SHOI2008] cactus仙人掌图
- 10163「BZOJ1492」[NOI2007] 货币兑换Cash
- 101432017 训练赛 1 by hzwer
- 9737「百度之星2017」程序设计大赛 初赛(B)
- 9005程序设计实习实验班2017作业(算法 作业1, 5)
- 8976「BZOJ1042」[HAOI2008] 硬币购物
- 8961「BZOJ1040」[ZJOI2008] 骑士
- 8816程序设计实习实验班2017推荐习题
- 8728算法设计与分析讨论班上机作业
- 8659「JoyOI1048」田忌赛马
- 8499NOIP2013花匠
- 8250「BZOJ3173」[TJOI2013] 最长上升子序列
- 8183「BZOJ1003」[ZJOI2006] 物流运输trans
- 79852015程序设计实习实验班免修考试(校内)
- 7844「BZOJ1046」[HAOI2007] 上升序列
- 7826数字三角形系列
- 7806「BZOJ1084」[SCOI2005] 最大子矩阵
- 7633「BZOJ1093」[ZJOI2007] 最大半连通子图
- 7540「BZOJ4008」[HNOI2015] 亚瑟王
- 7532「BZOJ4011」[HNOI2015] 落忆枫音
- 7318「BZOJ2326」[HNOI2011] 数学作业
- 7234「BZOJ2281」[SDOI2011] 黑白棋
- 7052「BZOJ1025」[SCOI2009] 游戏
- 6971NOI2005瑰丽华尔兹
- 6768「BZOJ3437」小P的牧场
- 6755[FJOI2014] 石子合并问题
- 6730「BZOJ2442」[Usaco2011 Open] 修剪草坪
- 64412015 ACM / ICPC EC - Final
- 6435NOIP2010乌龟棋
- 6434「BZOJ1177」[Apio2009] Oil
近期评论
- 匿名发表在《再见,OI》
- 匿名发表在《再见,OI》
- 匿名发表在《留言板》
- 匿名发表在《hzwer.com 博客导航》
- 匿名发表在《留言板》