CF-1215E

题意

$n(2\le n\le 4e5)$ 个物品,每个的颜色为 $a_i(1\le a_i\le 20)$ ,仅允许将相邻的物品两两交换,问使得相同颜色的物品聚集到一起的最小花费

题解

预处理 $tran[i][j]$ 表示仅考虑颜色 $i,j$ 时,将所有颜色为 $i$ 的物品移到所有颜色为 $j$ 的物品前面的花费,然后再使用状压 $dp$

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <bits/stdc++.h>
using namespace std;
#define forl(i, l, r) for (int i = l; i <= r; i++)
#define forr(i, r, l) for (int i = r; i >= l; i--)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define fro1(i, n) for (int i = 1; i <= n; i++)
#define for0(i, n) for (int i = 0; i < n; i++)
#define fro0(i, n) for (int i = 0; i < n; i++)
#define meminf(a) memset(a, inf, sz(a))
#define mem_1(a) memset(a, -1, sz(a))
#define mem0(a) memset(a, 0, sz(a))
#define memcp(a,b) memcpy(a,b,sz(b))
#define oper(type) bool operator <(const type Y)const
#define mp make_pair
#define pu_b push_back
#define pu_f push_front
#define po_b pop_back
#define po_f pop_front
#define fi first
#define se second
#define sz(a) (int)sizeof(a)
typedef pair<long long, long long> pll;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef double db;
typedef long double ldb;
typedef long long ll;
void in(initializer_list<int*> li){for(auto ptr=li.begin();ptr!=li.end();ptr++)scanf("%d",*ptr);}
void in(initializer_list<ll*> li){for(auto ptr=li.begin();ptr!=li.end();ptr++)scanf("%lld",*ptr);}
void in(initializer_list<db*> li){for(auto ptr=li.begin();ptr!=li.end();ptr++)scanf("%lf",*ptr);}
void out(initializer_list<int> li){auto ti=li.end();ti--;for(auto ptr=li.begin();ptr!=li.end();ptr++)printf("%d%c",*ptr,ptr==ti?'\n':' ');}
void out(initializer_list<ll> li){auto ti=li.end();ti--;for(auto ptr=li.begin();ptr!=li.end();ptr++)printf("%lld%c",*ptr,ptr==ti?'\n':' ');}
void out(initializer_list<db> li){auto ti=li.end();ti--;for(auto ptr=li.begin();ptr!=li.end();ptr++)printf("%f%c",*ptr,ptr==ti?'\n':' ');}
void out(int a,bool ln){printf("%d%c",a,ln?'\n':' ');}
void out(ll a,bool ln){printf("%lld%c",a,ln?'\n':' ');}
void out(db a,int digit,bool ln){printf("%.*f%c",digit,a,ln?'\n':' ');}
void out(ldb a,int digit,bool ln){printf("%.*Lf%c",digit,a,ln?'\n':' ');}
void out0(int a[],int n){for0(i,n)out(a[i],i==n-1);}
void out1(int a[],int n){for1(i,n)out(a[i],i==n);}
void out0(ll a[],int n){for0(i,n)out(a[i],i==n-1);}
void out1(ll a[],int n){for1(i,n)out(a[i],i==n);}
int in(int &a,int &b,int &c,int &d){return scanf("%d%d%d%d",&a,&b,&c,&d);}
int in(int &a,int &b,int &c){return scanf("%d%d%d",&a,&b,&c);}
int in(int &a,int &b){return scanf("%d%d",&a,&b);}
int in(ll &a,ll &b,ll &c,ll &d){return scanf("%lld%lld%lld%lld",&a,&b,&c,&d);}
int in(ll &a,ll &b,ll &c){return scanf("%lld%lld%lld",&a,&b,&c);}
int in(ll &a,ll &b){return scanf("%lld%lld",&a,&b);}
int in(ll &a){return scanf("%lld",&a);}
int in(int &a){return scanf("%d",&a);}
int in(char *s){return scanf("%s",s);}
int in(char &c){return scanf("%c",&c);}
int in(db &a){return scanf("%lf",&a);}
int in(ldb &a){return scanf("%Lf",&a);}
void in0(int a[],int n){for0(i,n)in(a[i]);}
void in1(int a[],int n){for1(i,n)in(a[i]);}
void in0(ll a[],int n){for0(i,n)in(a[i]);}
void in1(ll a[],int n){for1(i,n)in(a[i]);}
const db pi = acos(-1);
const db eps = 1e-8;
int sign(db a) {return a < -eps ? -1 : a > eps;}
int db_cmp(db a, db b){ return sign(a-b);}
int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;}// k3 在 [k1,k2] 内
int inf = 0x3f3f3f3f;
ll inf_ll = 0x3f3f3f3f3f3f3f3f;
ll mod = 1e9+7;
const int M = 2.1e5;
const int N = 2.1e5;
/*-----------------------------------head----------------------------------------------*/

ll tran[30][30];
vii node[30];
ll dp[1<<20];
int main() {
int n,x;
in(n);
for1(i,n){
in(x);
node[x-1].pu_b(i);
}
meminf(dp);
dp[0]=0;
for0(i,20)
for0(j,20){
if(i==j)continue;
int l=node[i].size(),r=node[j].size();
while(l&&r){
if(node[i][l-1]<node[j][r-1]){
r--;
}else{
tran[i][j]+=r;
l--;
}
}
}
for(int i=1;i<(1<<20);i++){
for0(j,20){
if(i&(1<<j)){
ll sum=0;
for0(k,20){
if(i&(1<<k))sum+=tran[j][k];
}
dp[i]=min(dp[i],dp[i-(1<<j)]+sum);
}
}
}
out(dp[(1<<20)-1],1);
return 0;
}