HDU5691 Sitting in Line【状压DP】
HDU5691 Sitting in Line
题意:
给出\(n\)个数字,有些数字的位置固定了,现在要求把所有没固定的数字放在一个位置,使得任意相邻两个位置的数字的相乘的和最大
题解:
\(n\)只有\(16\),考虑状压\(DP\)
\(DP[msk][i]\)表示当前已经选了\(msk\)集合里的数字且最后一个数字下标是\(i\)的最大值
view code
//#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 17;
const int INF = 0x3f3f3f3f;
int n,pos[MAXN],val[MAXN],A[MAXN],f[1<<16][MAXN],rps[MAXN];
void solve(int kase){
memset(f,-0x3f,sizeof(f));
memset(rps,0,sizeof(rps));
cin >> n;
for(int i = 1; i <= n; i++) cin >> val[i] >> pos[i];
for(int i = 1; i <= n; i++){
pos[i]++;
if(pos[i]) rps[pos[i]] = i;
}
f[0][0] = 0;
for(int msk = 1; msk < (1<<n); msk++){
int curpos = __builtin_popcount(msk); //当前位置
int prepos = curpos - 1; //上一个位置
for(int i = 1; i <= n; i++){ //枚举数字
if(!(msk&(1<<(i-1)))) continue; //不在集合里
if(rps[curpos]!=0 and (i!=rps[curpos])) continue; //该位置的数字已经固定
if(pos[i]!=0 and pos[i]!=curpos) continue; //该数字的位置已经固定
if(curpos==1){
f[msk][i] = 0;
continue;
}
for(int j = 1; j <= n; j++){
if(i==j or !(msk&(1<<(j-1)))) continue;
if(rps[prepos]!=0 and (j!=rps[prepos])) continue;
if(pos[j]!=0 and pos[j]!=prepos) continue;
if(f[msk^(1<<(i-1))][j]==-INF) continue;
f[msk][i] = max(f[msk][i],f[msk^(1<<(i-1))][j] + val[j] * val[i]);
}
}
}
cout << "Case #" << kase << ":\n";
cout << *max_element(begin(f[(1<<n)-1]),end(f[(1<<n)-1])) << endl;;
}
int main(){
____();
int t; cin >> t;
for(int kase = 1; kase <= t; kase++) solve(kase);
return 0;
}
HDU5691 Sitting in Line【状压DP】的更多相关文章
- hdu 5691 Sitting in Line 状压dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5691 题解: 和tsp用的状压差不多,就是固定了一些访问顺序. dp[i][j]表示前cnt个点中布 ...
- hdu_5691_Sitting in Line(状压DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5691 题意:中文,不解释 题解:设dp[i][j]表示当前状态为i,以第j个数为末尾的最忧解,然后dp ...
- hdu 5691 Sitting in line 状压动归
在本题中,n<=16n<=16n<=16, 不难想到可以将所选数字的编号进行状态压缩. 定义状态 dp[S][j]dp[S][j]dp[S][j],其中 SSS 代表当前所选出的所有 ...
- 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP
[BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...
- 【POJ3254】Corn Fields 状压DP第一次
!!!!!!! 第一次学状压DP,其实就是运用位运算来实现一些比较,挺神奇的.. 为什么要发“!!!”因为!x&y和!(x&y)..感受一下.. #include <iostre ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
- ZOJ3802 Easy 2048 Again (状压DP)
ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
- 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 ...
- poj3254 Corn Fields (状压DP)
http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
随机推荐
- Maven项目编译之后xml文件不存在
如题: 问题请看图,target目录是编译之后的,发现并没有对应的mapper.xml文件 原因: maven项目默认不加载此类文件 解决办法有两个: 其一是将mybatis的xml映射文件放在mav ...
- 【MySQL 高级】架构介绍
MySQL高级 架构介绍 MySQL 简介 MySQL 安装 Docker 安装 参考链接 Linux 安装 参考链接 MySQL 配置文件 log-bin:二进制日志文件.用于主从复制.它记录了用户 ...
- 【Spring】Spring 事务控制
Spring 事务控制 Spring 事务控制介绍 JavaEE 体系进行分层开发,事务控制位于业务层,Spring 提供了分层设计业务层的事务处理解决方案. Spring 的事务控制都是基于 AOP ...
- (二)数据源处理2-xlrd操作excel
import xlrd3workbook = xlrd3.open_workbook('test_data.xlsx')sheet =workbook.sheet_by_name('Sheet1')p ...
- html2canvas canvas webgl 截图透明空🤣
1. React用这个插件html2canvas完成div截图功能,div里面嵌套canvas,返回base64是透明图片. html2canvas(document.getElementById(& ...
- mysql—information_schema数据库
一.介绍 MySQL中有一个默认数据库名为information_schema,在MySQL中我们把 information_schema 看作是一个数据库,确切说是信息数据库.其中保存着关于MySQ ...
- ctfhub技能树—信息泄露—备份文件下载—.DS_Store
打开靶机 查看页面信息 使用dirsearch进行扫描 访问该网页,下载文件 使用Linux系统打开文件 发现一个特殊文件,使用浏览器打开 拿到flag 二.使用Python-dsstore工具查看该 ...
- Table controls and tabstrip controls
本文转载自http://www.cnblogs.com/clsoho/archive/2010/01/21/1653268.html ONTROLS Syntax Forms Declaration ...
- atlas读写分离
Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bug ...
- file转化为binary对象发送给后台
具体代码如下: function filechange(e) { var file = $('#filed').get(0).files[0]; var fileSize = file.size, f ...