还蛮有趣的一道状态DP的题目。

 /* 435B */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
int mask[maxn];
bool visit[maxn];
int P[maxn];
const int m = ;
const int N = (<<m)+;
const int INF = 0x1f1f1f1f;
int dp[][N];
int path[][N];
int a[]; void init() {
int i, j, k = ; for (i=; i<maxn; ++i) {
if (!visit[i]) {
P[k++] = i;
for (j=i*i; j<maxn; j+=i)
visit[j] = true;
}
} for (i=; i<maxn; ++i) {
for (j=; j<m; ++j) {
if (i%P[j] == ) {
mask[i] |= (<<j);
}
}
} #ifndef ONLINE_JUDGE
printf("P[%d] = %d\n", m, P[m]);
#endif
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif init();
int n; scanf("%d", &n);
rep(i, , n+)
scanf("%d", &a[i]); int i, j, k;
int st, tmp;
int mxs = <<; memset(dp, 0x1f, sizeof(dp));
memset(dp[], , sizeof(int)*N);
for (i=; i<=n; ++i) {
for (j=; j<mxs; ++j) {
for (k=; k<=; ++k) {
if (j & mask[k])
continue;
tmp = dp[i-][j] + abs(a[i] - k);
st = j | mask[k];
if (dp[i][st] > tmp) {
dp[i][st] = tmp;
path[i][st] = k;
}
}
}
} int mn = INT_MAX, v; for (j=; j<mxs; ++j) {
if (dp[n][j] < mn) {
mn = dp[n][j];
v = j;
}
} vi ans; for (i=n; i>; --i) {
k = path[i][v];
ans.pb(k);
v ^= mask[k];
} for (i=n-; i>=; --i)
printf("%d ", ans[i]);
putchar('\n'); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【CF】259 Div.1 B Little Pony and Harmony Chest的更多相关文章

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

    题目范围给的很小,所以有状压的方向. 我们是构造出一个数列,且数列中每两个数的最大公约数为1; 给的A[I]<=30,这是一个突破点. 可以发现B[I]中的数不会很大,要不然就不满足,所以B[I ...

  2. 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 ...

  3. 【CF】121 Div.1 C. Fools and Roads

    题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...

  4. 【CF】310 Div.1 C. Case of Chocolate

    线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...

  5. 【CF】110 Div.1 B. Suspects

    这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...

  6. 【CF】222 Div.1 B Preparing for the Contest

    这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心.对时间二分, 对费用采用贪心. /* 377B */ #include <iostream> #include ...

  7. 【CF】207 Div.1 B.Xenia and Hamming

    这题目一看很牛逼,其实非常easy.求求最小公倍数,最大公约数,均摊复杂度其实就是O(n). /* 356B */ #include <iostream> #include <str ...

  8. 【CF】142 Div.1 B. Planes

    SPFA.注意状态转移条件,ans的求解需要在bfs中间求解.因为只要到了地点n,则无需等待其他tourist.还是蛮简单的,注意细节. /* 229B */ #include <iostrea ...

  9. 【CF】196 Div.2 D. Book of Evil

    显然这个图是一课树,看着题目首先联想到LCA(肯定是可以解的).但是看了一下数据大小,应该会TLE.然后,忽然想到一个前面做过的题目,大概是在一定条件下树中某结点旋转成为根后查询最长路径.结果灵感就来 ...

随机推荐

  1. Redis Cache 简介

    Microsoft Azure Redis Cache 是基于流行的开源Redis Cache 1.Microsoft Azure Redis Cache 可分为以下几个级别: Basic – 单节点 ...

  2. Chrome资源加载被Cancel的问题

    好个表久没写文章了. 做为一个砖业的砖工只能天天搬砖做些没有营养没有技术难度的活儿. 最近折腾个网站.发现一个很奇怪的问题- 各种图片的请求被取消了状态为Canceled. 顿时Chrome变成一个更 ...

  3. ACM——A + B Problem (1)

    A + B Problem (1) 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:5907            测试通过:151 ...

  4. 关于ligerUI中ligerTree代码中的一个bug,造成该控件无法通过url的POST方式加载数据

    该bug造成ligerTree参数中的method无论你怎么设置都只能用get方式提交 由于本人水平有限,只是找到原因,但无法修正 ligerUI v1.1.9 版本中的ligerui.all.js文 ...

  5. 1066. Root of AVL Tree

    An AVL tree is a self-balancing binary search tree.  In an AVL tree, the heights of the two child su ...

  6. Extjs事件继承注意事项

    Extjs事件继承总结: 在基类中只需配置通用事件,无需配置通用界面,通用界面无效,通用事件一直有效 基表格控制器

  7. iOS开发——免证书调试(Xcode7,iOS9)

    (资料已做好,待整理成文章……)

  8. /Users/XX/Library/Developer/Xcode/DerivedData/XX.app/xxsdk.bundle Directory not empty

    今天在升级xcode后真机调试偶然发现这个问题,查了一些资料发现还是不能完全解决 解决方法:参考的(http://blog.csdn.net/alincexiaohao/article/details ...

  9. 迭代map方法

    Map<String, String> map=new HashMap<String,String>();map.put("1", "one&qu ...

  10. 计算字符串和文件的MD5值

    //计算字符串的MD5值 public string GetMD5(string sDataIn) { MD5CryptoServiceProvider md5 = new MD5CryptoServ ...