跳到正文

题解

题解 CF1215E 【Marbles】

Gu_Pigeon约 7 分钟
原文
文章信息与版本

文章信息

发布
2019年9月16日
收录
2026年11月15日
更新
2026年11月17日
篇幅
约 7 分钟
获赞
11
题目
CF1215E

版本

  1. v1当前

前置知识:

  • 状态压缩DP
  • 逆序对(或许不用)

原题:

Monocarp has arranged nn colored marbles in a row. The color of the ii-th marble is aia_i. Monocarp likes ordered things, so he wants to rearrange marbles in such a way that all marbles of the same color form a contiguos segment (and there is only one such segment for each color).

In other words, Monocarp wants to rearrange marbles so that, for every color jj, if the leftmost marble of color jj is ll-th in the row, and the rightmost marble of this color has position rr in the row, then every marble from ll to rr has color jj.

To achieve his goal, Monocarp can do the following operation any number of times: choose two neighbouring marbles, and swap them.

You have to calculate the minimum number of operations Monocarp has to perform to rearrange the marbles. Note that the order of segments of marbles having equal color does not matter, it is only required that, for every color, all the marbles of this color form exactly one contiguous segment.

Input

The first line contains one integer nn (2≤n≤4⋅105)(2 \le n \le 4 \cdot 10^5) — the number of marbles.

The second line contains an integer sequence a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤20)(1 \le a_i \le 20), where aia_i is the color of the ii-th marble.

Output

Print the minimum number of operations Monocarp has to perform to achieve his goal.

题意:

给一个长度为n的序列,求出最少交换相邻数字的次数,使得相同的数字在一条连续的线段中。

其中2≤n≤4⋅1052 \le n \le 4 \cdot 10^5, 对于其中每个数字(1≤ai≤20)(1 \le a_i \le 20)

束手无策?一脸懵逼?赛场上我也是这样

如果稍稍修改一下题目要求呢?

“如果题目要求最终的颜色要从小到大排序就好了,这样就只用求逆序对了……”

That’s it!\textit{\textsf{That's it!}}

所以我们只需要:

枚举每一个数字完成交换后的位置

时间复杂度 O(a!)\text O(a!),妥妥 TLE\small\color{#052242}\textsf{TLE}

所以:

bang\tiny\textsf{bang}
我该怎么办呢

我们考虑将每一种数字按最终答案里的顺序,向左挪动,直至到达其在最终答案的位置,在该过程中,显然会形成一个前半部分已经“排序”完毕而后半部分需要解决的序列

而我们只需要使得后半部分的待解决序列中的数字转移到已解决序列中,就可以解决这一个子问题,转移到一个新的状态

所以,我们换一个状态枚举:数字 aia_i 是否已经“排序”

用0来表示该数字还未处理,1表示已经有序。

然后,就是正解的登场时间:

状压DP\textbf{状压DP}

用一个二进制数 i 来表示状态,则有转移方程:

f[i] = f[i - (1 << j)] + 所有数值为 j 的位置转移到前半部分需要跨过的其他数字个数

那么到此这题就结束了还有一个问题:如何求出上式的

所有数值为 j 的位置转移到前半部分需要跨过的其他数字个数

呢?

预处理就好咯。。。

用数组 pr[20][20] 记录某一种值的所有数字的左边有多少个另一种数字

有点绕……其实 pr[x][y] 记录了所有的数对 (i, j) ,使得 i > j 且 a[i] == x && a[j] == y 的个数,可以结合代码理解一下……

那么将一类数字挪到左半部分,只需要跨过右半部分的数字就可以了。

那么真正的状态转移方程如下:

f[i] = f[i - (1 << j)] + sigma(pr[j][k]) ,其中 k 要满足状态 i 的第 k 位为 0

那么,本题也就算是通过了。又双叒叕AC了一道灰题,真开心

Code Below\large\textit{Code Below}

C++
#include <bits/stdc++.h>
using namespace std;
#define N 524288
#define int long long //反正都要开ll,不如直接define(
long long f[1 << 20], pr[30][30], n, a[N], mp[30], tot, cnt[30];

signed main()
{
	scanf("%lld", &n);
	memset(mp, -1, sizeof mp);
	for (int i = 1; i <= n; i++) 
	{
		scanf("%lld", a + i);
		if (!~mp[a[i]]) mp[a[i]] = tot++;
	}//在实际的实现中,我使用了映射,使数字范围从零开始,方便dp
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j < tot; j++) 
			pr[mp[a[i]]][j] += cnt[j];
		cnt[mp[a[i]]]++;
	}//通过奇(xia)怪(gao)方式计算出pr数组
	memset(f, 0x3f, sizeof f);//初始化dp数组
	f[0] = 0;//边界条件
	for (int i = 1; i < 1 << tot; i++)
	{
		for (int j = 0; j < tot; j++)
		{
			if ((i >> j) & 1)
			{
				int tmp = 0;
				for (int k = 0; k < tot; k++)
					if (!((i >> k) & 1) && j != k) tmp += pr[j][k];
				f[i] = min(f[i], f[i ^ (1 << j)] + tmp);//上述转移方程
			}
		}
	}
	printf("%lld", f[(1 << tot) - 1]);//最终答案即为所有数字都进入左半部分
}

首次通过了题目 Marbles ,恭喜你!(

原文
存档评论3 条抓取于