Codeforces Round #585 (Div. 2) E. Marbles(状压dp)
题意:给你一个长度为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)的更多相关文章
- CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP
题目传送门 https://codeforces.com/contest/1103/problem/D 题解 失去信仰的低水平选手的看题解的心路历程. 一开始看题目以为是选出一些数,每个数可以除掉一个 ...
- Codeforces Round #585 (Div. 2) E. Marbles (状压DP),BZOJ大理石(同一道题)题解
题意 林老师是一位大理石收藏家,他在家里收藏了n块各种颜色的大理石,第i块大理石的颜色为ai.但是林老师觉得这些石头在家里随意摆放太过凌乱,他希望把所有颜色相同的石头放在一起.换句话说,林老师需要对现 ...
- 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 Round #585 (Div. 2) E. Marbles (状压DP)
题目:https://codeforc.es/contest/1215/problem/E 题意:给你一个序列,你可以交换相邻的两个数,要达到一个要求,所有相同的数都相邻,问你交换次数最少是多少 思路 ...
- Codeforces Round #585 (Div. 2)E(状态压缩DP,思维)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h>using namespace std;long long n,x;long lon ...
- Codeforces Round #585 (Div. 2)
https://www.cnblogs.com/31415926535x/p/11553164.html 感觉很硬核啊这场,,越往后越做不动,,,emmmm,,,(这场是奔着最后一题 2sat 来的, ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #222 (Div. 1) C. Captains Mode 对弈+dp
题目链接: http://codeforces.com/contest/378/problem/E 题意: dota选英雄,现在有n个英雄,m个回合,两支队伍: 每一回合两个选择: b 1,队伍一ba ...
- 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 ...
随机推荐
- PostgreSQL WAL日志详解
wal日志即write ahead log预写式日志,简称wal日志.wal日志可以说是PostgreSQL中十分重要的部分,相当于oracle中的redo日志. 当数据库中数据发生变更时:chang ...
- fastjson复现项目代码
详情请见:https://www.cnblogs.com/yunmuq/p/14268028.html 以下是代码 // FastjsonDemo.java package test; import ...
- 区间合并 C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; ty ...
- spring cloud gateway 日志打印
从api请求中获取访问的具体信息,是一个很常见的功能,这几天在研究springcloud,使用到了其中的gateway,刚好将研究的过程结果都记录下来 0. Version <parent> ...
- MySQL常用的数据类型和字段属性
数据类型 数值 tinyint 十分小的数据 1个字节 smallint 较小的数据 2个字节 mediumint 中等大小的数据 3个字节 int 标准的整数 4个字节 常用 bigint 较大的数 ...
- 【IMP】导出的时候显示ddl建表语句
导出数据后,在导入的时候想要显示出建表语句,可以用show=y这个选项来实现 imp test/test file=test.dmp ignore=y show=y fromuser=test1 to ...
- nodejs中使用worker_threads来创建新的线程
目录 简介 worker_threads isMainThread MessageChannel parentPort和MessagePort markAsUntransferable SHARE_E ...
- 本地Mac通过堡垒机代理实现跨堡垒机scp问题
近日,公司在跳板机前架设了堡垒机,以防止ssh攻击,但这带来一个问题,我们平常直接ssh跳板机,可以直接使用scp来上传或下载跳板机数据到本地 架设堡垒之后经常使用的scp工具不好用了 于是本期就来解 ...
- JavaScript 实现排序算法
参考文章: 十大经典排序算法动画,看我就够了! 1. 冒泡排序 思路 比较所有相邻元素,如果第一个比第二个大,则交换它们 一轮下来,可以保证最后一个数是最大的 执行n-1轮,就可以完成排序 代码 Ar ...
- Obligations for calling close() on the iterable returned by a WSGI application
Graham Dumpleton: Obligations for calling close() on the iterable returned by a WSGI application. ht ...