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】的更多相关文章

  1. hdu 5691 Sitting in Line 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5691 题解: 和tsp用的状压差不多,就是固定了一些访问顺序. dp[i][j]表示前cnt个点中布 ...

  2. hdu_5691_Sitting in Line(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5691 题意:中文,不解释 题解:设dp[i][j]表示当前状态为i,以第j个数为末尾的最忧解,然后dp ...

  3. hdu 5691 Sitting in line 状压动归

    在本题中,n<=16n<=16n<=16, 不难想到可以将所选数字的编号进行状态压缩. 定义状态 dp[S][j]dp[S][j]dp[S][j],其中 SSS 代表当前所选出的所有 ...

  4. 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP

    [BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...

  5. 【POJ3254】Corn Fields 状压DP第一次

    !!!!!!! 第一次学状压DP,其实就是运用位运算来实现一些比较,挺神奇的.. 为什么要发“!!!”因为!x&y和!(x&y)..感受一下.. #include <iostre ...

  6. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

  7. ZOJ3802 Easy 2048 Again (状压DP)

    ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

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

  9. poj3254 Corn Fields (状压DP)

    http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

随机推荐

  1. 【函数分享】每日PHP函数分享(2021-1-7)

    ltrim() 删除字符串开头的空白字符(或其他字符). string ltrim ( string $str[, string $character_mask]) 参数描述str 输入的字符串. c ...

  2. ssh升级以及ssh: symbol lookup error: ssh: undefined symbol: EVP_aes_128_ctr错误处理

    1.解压安装openssl包:(不能卸载openssl,否则会影响系统的ssl加密库文件,除非你可以做两个软连接libcryto和libssl) # tar -zxvf openssl-1.0.1.t ...

  3. Petalinux和Vivado的安装

    Petalinux和Vivado的安装 背景 我是搞软件的, FPGA这块不太了解.由于机缘巧合,最近有接触到这块的开发.所以先挖一坑. 先声明我不是专业搞这块的,所以对这块的内容理解可能会有偏差,以 ...

  4. 【MyBatis】MyBatis 动态 SQL

    MyBatis 动态SQL if 可以根据实体类的不同取值,使用不同的 SQL 语句来进行查询. 使用动态 SQL 最常见情景是根据条件包含 where 子句的一部分. 持久层 DAO 接口: pub ...

  5. 开源:AspNetCore 应用程序热更新升级工具(全网第一份公开的解决方案)

    1:下载.开源.使用教程 下载地址:Github 下载 .其它下载 开源地址:https://github.com/cyq1162/AspNetCoreUpdater 使用教程: 解压AspNetCo ...

  6. firewalld原理和基础命令

    firewalld防火墙 Firewalld是什么? Firewalld提供了支持网络.防火墙定义网络看见以及接口安全等级的动态防火墙管理工具

  7. python7、8章

    目录 第七章 用户输入和while循环 7.1 函数input()的工作原理 7.1.1 编写清晰的程序 7.1.2 使用int()来获取数值输入 分析: 结果: 7.1.3 求模运算符 7.1.4 ...

  8. 转 10 jmeter之动态关联

    10 jmeter之动态关联   jmeter中关联是通过之前请求的后置处理器实现的,具体有两种方式:XPath Extractor(一般xml的时候用的多)和正则表达式提取器. 以webtours登 ...

  9. iDRAC RAC0218 最大会话数

    戴尔服务器IDRAC能ping通,但是网页打不开的时候: 用putty登录: /admin1-> racadm racreset RAC reset operation initated suc ...

  10. 每天学一点 Vue3(一) CND方式的安装以及简单使用

    简介 感觉vue3的新特性很舒服,这样才是写软件的感觉嘛.打算用Vue实现自己的一些想法. Vue3还有几个必备库,比如Vue-Router(负责路由导航).Vuex(状态管理.组件间通信),还有第三 ...