Codeforces 279D The Minimum Number of Variables 状压dp
The Minimum Number of Variables
我们定义dp[ i ][ mask ]表示是否存在 处理完前 i 个a, b中存者 a存在的状态是mask 的情况。
然后用sosdp处理出,状态为state的a, 能组成的数字, 然后转移就好啦。
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n;
int a[N]; int cnt[ << ];
int sos[ << ];
bool dp[][ << ]; int main() {
scanf("%d", &n);
for(int i = ; i < ( << n); i++)
cnt[i] = cnt[i - (i & - i)] + ;
for(int i = ; i < n; i++) scanf("%d", &a[i]);
for(int i = ; i < n; i++) {
for(int j = i; j < n; j++) {
int ret = a[i] + a[j];
for(int k = ; k < n; k++) {
if(ret == a[k])
sos[( << i) | ( << j)] |= << k;
}
}
}
for(int i = ; i < n; i++)
for(int mask = ; mask < ( << n); mask++)
if(mask >> i & ) sos[mask] |= sos[mask ^ ( << i)]; dp[][] = ;
for(int i = ; i < n - ; i++) {
for(int mask = ; mask < ( << n); mask++) {
if(!dp[i][mask]) continue;
if(sos[mask] >> (i + ) & ) {
dp[i + ][mask | ( << (i + ))] = true;
for(int j = ; j <= i; j++) {
if(mask >> j & ) {
dp[i + ][(mask ^ ( << j)) | ( << (i + ))] = true;
}
}
}
}
}
int ans = inf;
for(int i = ; i < ( << n); i++)
if(dp[n - ][i]) chkmin(ans, cnt[i]);
if(ans == inf) ans = -;
printf("%d\n", ans);
return ;
} /*
*/
Codeforces 279D The Minimum Number of Variables 状压dp的更多相关文章
- CodeForces 279D The Minimum Number of Variables 题解
题目大意: 有一组n个不相同的数字组成数串:a1,a2,a3-an. 1.一个数组b. 2.第一个操作我们将b0的值赋为a1.之后我们有n-1个操作,第k次操作我们将by=bi+bj(y,i,j可能相 ...
- 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\) ...
- CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP
题目传送门 https://codeforces.com/contest/1103/problem/D 题解 失去信仰的低水平选手的看题解的心路历程. 一开始看题目以为是选出一些数,每个数可以除掉一个 ...
- codeforces#580 D. Kefa and Dishes(状压dp)
题意:有n个菜,每个菜有个兴奋值,并且如果吃饭第i个菜立即吃第j个菜,那么兴奋值加ma[i][j],求吃m个菜的最大兴奋值,(n<=18) 分析:定义dp[status][last],statu ...
- Codeforces Round #585 (Div. 2) E. Marbles(状压dp)
题意:给你一个长度为n的序列 问你需要多少次两两交换 可以让相同的数字在一个区间段 思路:我们可以预处理一个数组cnt[i][j]表示把i放到j前面需要交换多少次 然后二进制枚举后 每次选择一个为1的 ...
- Codeforces Beta Round #8 C. Looking for Order 状压dp
题目链接: http://codeforces.com/problemset/problem/8/C C. Looking for Order time limit per test:4 second ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
随机推荐
- 浅谈Kubernetes生产架构
注意本文,只是笔者针对Kubernetes生产环境运行的一些关于架构设计和实现方案的总结,内容很粗糙,同时也会不断完善. 首先,我们来梳理下Kubernetes生产架构,其设计适用于绝大多数环境.如下 ...
- Kafka运行一段时间报错Too many open files
Kafka运行一段时间报错Too many open files 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.kafka运行一段时间报错 1>.我最近把kafka集群 ...
- Eclipse MAT 安装及使用
Eclipse MAT官方网页:https://www.eclipse.org/mat/downloads.php 一.MAT是什么? MAT(Memory Analyzer Tool),一个基于Ec ...
- hadoop记录-如何换namenode机器
namenode机器磁盘IO负载持续承压,造成NAMENODE切换多次及访问异常. 1 初始化新机器1.1 在新器1.1.1.3部署hadoop软件(直接复制standby1.1.1.2节点)1.2 ...
- Mac 下 Eclipse 添加 Dynamic Web Project 并配置 Tomcat
最近拿到了一个 Dynamic Web Project,我的 Mac 上的 Eclipse 之前没有过这类型的项目,所以导入之后无法正常运行.下面是我记录的如何配置 Eclipse 使之能够运行 Dy ...
- HTML界面多语言切换
我在做个人网站时考虑到多语言转换的问题,最后在JS文件中将所有文字数据储存然后设置HTML中的class与key属性来达到多语言效果.当然,还有一个别的方法,就是所有有文字内容的div都用不同语言写一 ...
- postgreSql 基本操作总结
0. 启动pgsl数据库 pg_ctl -D /xx/pgdata start 1. 命令行登录数据库 1 psql -U username -d dbname -h hostip -p po ...
- html常用标签的取值和赋值操作
我们在html页面当中,面对各种各样的标签,经常需要处理取值和赋值的问题,下面,就把常见的一些html标签元素的取值和赋值操作进行总结整理,以后备用. 1.button:改变button按钮上面的值, ...
- Lucene的中文分词器
1 什么是中文分词器 学过英文的都知道,英文是以单词为单位的,单词与单词之间以空格或者逗号句号隔开. 而中文的语义比较特殊,很难像英文那样,一个汉字一个汉字来划分. 所以需要一个能自动识别中文语义的分 ...
- gunicorn+anaconda+nginx部署django项目(ubuntu)
首先进入conda 虚拟环境: source activate test 安装gunicorn: pip install gunicorn 运行gunicorn gunicorn -w 2 -b 12 ...