题意:给你一个长度为n的序列 问你需要多少次两两交换 可以让相同的数字在一个区间段

思路:我们可以预处理一个数组cnt[i][j]表示把i放到j前面需要交换多少次 然后二进制枚举后 每次选择一个为1的位置 考虑这个位置最后加进来的花费取最小

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int N = 4e5+7;
typedef long long ll;
const ll mod = 1e9+7;
using namespace std;
int a[N];
ll cnt[30][30];
vector<int> v[30];
ll dp[1<<20];
void work(){
for(int i=1;i<=20;i++)
for(int j=1;j<=20;j++){
if(i==j) continue;
if(v[j].size()==0||v[i].size()==0){
continue;
}
int po1,po2; po1=po2=0;
for(po1=0;po1<v[i].size();po1++){
// cout<<v[i].size()<<endl;
while(1){
// cout<<po1<<" "<<po2<<endl;
if(po2==v[j].size()-1||v[j][po2+1]>v[i][po1]) break;
po2++;
// cout<<"1"<<endl;
}
if(v[i][po1]>v[j][po2]){
cnt[i][j]+=(po2+1);
// cout<<po2+1<<" ";
}
// cout<<endl;
}
// cout<<cnt[i][j]<<" "<<i<<" "<<j<<endl;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n; cin>>n;
for(int i=1;i<=n;i++) cin>>a[i],v[a[i]].push_back(i);
work();
for(int i=0;i<(1<<20);i++) dp[i]=0x3f3f3f3f3f3f3f3f;
dp[0]=0;
for(int i=1;i<(1<<20);i++){
for(int j=0;j<20;j++){
if(i&(1<<j)){
ll sum=0;
for(int k=0;k<20;k++){
if(k==j) continue;
if(i&(1<<k))
sum+=cnt[k+1][j+1];
}
dp[i]=min(dp[i],dp[i^(1<<j)]+sum);
}
}
}
cout<<dp[(1<<20)-1]<<endl;
return 0;
}

Codeforces Round #585 (Div. 2) E. Marbles(状压dp)的更多相关文章

  1. CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP

    题目传送门 https://codeforces.com/contest/1103/problem/D 题解 失去信仰的低水平选手的看题解的心路历程. 一开始看题目以为是选出一些数,每个数可以除掉一个 ...

  2. Codeforces Round #585 (Div. 2) E. Marbles (状压DP),BZOJ大理石(同一道题)题解

    题意 林老师是一位大理石收藏家,他在家里收藏了n块各种颜色的大理石,第i块大理石的颜色为ai.但是林老师觉得这些石头在家里随意摆放太过凌乱,他希望把所有颜色相同的石头放在一起.换句话说,林老师需要对现 ...

  3. Educational Codeforces Round 13 E. Another Sith Tournament 状压dp

    E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...

  4. Codeforces Round #585 (Div. 2) E. Marbles (状压DP)

    题目:https://codeforc.es/contest/1215/problem/E 题意:给你一个序列,你可以交换相邻的两个数,要达到一个要求,所有相同的数都相邻,问你交换次数最少是多少 思路 ...

  5. Codeforces Round #585 (Div. 2)E(状态压缩DP,思维)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h>using namespace std;long long n,x;long lon ...

  6. Codeforces Round #585 (Div. 2)

    https://www.cnblogs.com/31415926535x/p/11553164.html 感觉很硬核啊这场,,越往后越做不动,,,emmmm,,,(这场是奔着最后一题 2sat 来的, ...

  7. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  8. Codeforces Round #222 (Div. 1) C. Captains Mode 对弈+dp

    题目链接: http://codeforces.com/contest/378/problem/E 题意: dota选英雄,现在有n个英雄,m个回合,两支队伍: 每一回合两个选择: b 1,队伍一ba ...

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

随机推荐

  1. WPF + RDLC + 动态生成列 + 表头合并

    如下,评论超过20条,马上发代码*(੭*ˊᵕˋ)੭*ଘ,效果如下: 代码逻辑简单. WPF使用RDLC需要使用如下DLL 新建WPF 窗体,黏贴下大概如下 <Window xmlns:rv=&q ...

  2. JavaScript高级程序设计(第4版)知识点总结

    介绍 JavaScript高级程序设计 第四版,在第三版的基础上添加了ES6相关的内容.如let.const关键字,Fetch API.工作者线程.模块.Promise 等.适合具有一定编程经验的 W ...

  3. jenkins 构建历史 显示版本号

    0   jenkins 安装此插件: 此插件名为 " groovy postbuild " 1  效果图: 2   安装插件: 系统管理 --> 插件管理 --> 可选 ...

  4. Linux性能相关命令

    Linux性能相关命令 目录 Linux性能相关命令 1. 查看硬盘相关信息 2. 查看CPU相关信息 3. 查看内存相关信息 4. 查看进程运行的信息 1. 查看硬盘相关信息 cat /proc/s ...

  5. 【Oracle】regexp_substr()函数详解

    环境:Oracle10.2.0.5 在SQL中尝试使用正则 可以试下regexp_substr()来进行分割 首先创建一个实验视图: SQL> create or replace view te ...

  6. MySQL全面瓦解17:触发器相关

    关于触发器 现实开发中我们经常会遇到这种情况,比如添加.删除和修改信息的时候需要记录日志,我们就要在完成常规的数据库逻辑操作之后再去写入日志表,这样变成了两步操作,更复杂了. 又比如删除一个人员信息的 ...

  7. mysql查看修改参数

    1.查看参数 show variables like '%timeout%'; 2.修改参数 会话级别修改: set session innodb_lock_wait_timeout=50; 对当前会 ...

  8. windows下的:开始→运行→命令

    开始→运行→命令 集锦                          winver---------检查Windows版本wmimgmt.msc----打开windows管理体系结构(WMI)wu ...

  9. ABAP program lines are wider than the internal table

    错误详细描述: An exception occurred that is explained in detail below.The exception, which is assigned to ...

  10. mybatis中传集合时 报异常 invalid comparison: java.util.Arrays$ArrayList and java.lang.String

    犯了一个低级的错误,在传集合类型的参数时,把他当成字符串处理了,导致报类型转换的错误 把  and nsrsbh!=' ' 删掉就行了