Description:

给定一个序列 \(a_i\) ,每次可以交换相邻两个元素,求使序列变成若干个极大连续段,每个极大连续段内部的值相同且任意两个极大连续段的值互不相同。

\(n\le 4\times 10^5, a_i\le 20\)

Solution:

由于值域很小,启发我们从值域入手,考虑每一种颜色。

设 \(cnt[i][j]\) 表示在只考虑颜色 \(i\) 和 \(j\) 的情况下,把所以颜色 \(i\) 都移到所有颜色 \(j\) 的前面的步数,这个对每一个颜色用 \(\text{vector}\) 存下出现的位置(从小到大),用 \({\rm two\ pointers}\) 扫一下即可求得 。

考虑状压每一种颜色,二进制下为1表示这个位的颜色已经被安排好了。

设 \(dp[S]\) 表示 \(S\) 的颜色已经安排好在序列的前面且两两不交的最小步数,那么转移就枚举一个不在 \(S\) 中的点 \(i\) ,把 \(i\) 放在考虑完的 \(S\) 的后面,那么产生的步数就是把 \(S\) 放在 \(i\) 的前面的步数,通过 \(cnt[][i]\) 可以求得。

Code:

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <fstream> typedef long long LL;
typedef unsigned long long uLL; #define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define MP(x, y) std::make_pair(x, y)
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define GO cerr << "GO" << endl; using namespace std; inline void proc_status()
{
ifstream t("/proc/self/status");
cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
} template<class T> inline T read()
{
register T x(0);
register char c;
register int f(1);
while (!isdigit(c = getchar())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c xor 48), isdigit(c = getchar()));
return x * f;
} template<typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; } const int maxN = 4e5;
const int maxM = 20; int n;
vector<int> col[maxM + 1];
LL cnt[maxM + 1][maxM + 1];
LL dp[1 << maxM]; void Input()
{
n = read<int>();
for (int i = 1; i <= n; ++i)
{
int x = read<int>();
col[x].push_back(i);
}
} void Init()
{
for (int i = 1; i <= 20; ++i)
for (int j = 1; j <= 20; ++j)
if (i != j)
{
if (!col[i].size() || !col[j].size()) continue;
int l = 0;
for (int k = 0; k < SZ(col[i]); ++k)
{
while (true)
{
if (l == SZ(col[j]) - 1 || col[j][l + 1] > col[i][k])
break;
l++;
}
if (col[j][l] < col[i][k])
cnt[i][j] += l + 1;
}
}
} void Solve()
{
memset(dp, 0x3f, sizeof dp); dp[0] = 0;
for (int S = 0; S < 1 << maxM; ++S)
{
for (int i = 0; i < maxM; ++i)
if (!(S >> i & 1))
{
LL sum(0);
for (int j = 0; j < maxM; ++j)
if (S >> j & 1)
sum += cnt[j + 1][i + 1];
chkmin(dp[S | (1 << i)], dp[S] + sum);
}
} cout << dp[(1 << maxM) - 1] << endl;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("CF585E.in", "r", stdin);
freopen("CF585E.out", "w", stdout);
#endif
Input();
Init();
Solve();
return 0;
}

[CF585E]Marbles的更多相关文章

  1. Marbles启动信息

    body-parser deprecated undefined extended: provide extended option app.js:40:20 -------------------- ...

  2. UVA 10090 - Marbles 拓展欧几里得

    I have some (say, n) marbles (small glass balls) and I am going to buy some boxes to store them. The ...

  3. Codeforces Round #336 Marbles

    E. Marbles time limit per test:  2 seconds memory limit per test:  256 megabytes input:  standard in ...

  4. uva 10090 Marbles

    Problem F Marbles Input: standard input Output: standard output I have some (say, n) marbles (small ...

  5. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  6. CF585E. Present for Vitalik the Philatelist [容斥原理 !]

    CF585E. Present for Vitalik the Philatelist 题意:\(n \le 5*10^5\) 数列 \(2 \le a_i \le 10^7\),对于每个数\(a\) ...

  7. 基于ubuntu16.04部署IBM开源区块链项目-弹珠资产管理(Marbles)

    前言 本教程基本上是对Marbles项目的翻译过程. 如果英文比较好的话,建议根据官方操作说明,一步步进行环境部署.当然你也可以参考本教程在自己的主机上部署该项目. Marbles 介绍 关于 Mar ...

  8. UVA 10090 Marbles(扩展欧几里得)

    Marbles Input: standard input Output: standard output I have some (say, n) marbles (small glass ball ...

  9. 【AtCoder】ARC086 E - Smuggling Marbles

    [题目]E - Smuggling Marbles [题意]给定n+1个点的树(root=0),每个点可以选择放或不放弹珠,每一轮顺序进行以下操作: 1.将根节点0的弹珠加入答案. 2.每个点的弹珠移 ...

随机推荐

  1. JAVA笔记19-容器之三 Set接口、List接口、Collections类、Comparable接口(重要)

    一.Set接口 //HashSet综合举例 import java.util.*; public class Test{ public static void main(String[] args){ ...

  2. [LOJ 6704] 健身计划

    问题描述 九条可怜是一个肥胖的女孩. 她最近长胖了,她想要通过健身达到减肥的目的,于是她决定每天做n次仰卧起坐以达到健身的目的. 她可以将这n次动作分为若干组完成,每一次完成ai次仰卧起坐,每做完一次 ...

  3. CF1263F

    题目描述 给出一个类似这样 的图,求删掉最多的黑边使得每个特殊点和至少一个节点1连通 保证上下两棵树都存在一种dfs序使得访问特殊点的顺序为1~n 题解 设f[i][j]表示上面的树最后一个特殊点为i ...

  4. floor函数用法

    floor(x),也写做Floor(x),其功能是“向下取整”,或者说“向下舍入”,即取不大于x的最大整数(与“四舍五入”不同,下取整是直接取按照数轴上最接近要求值的左边值,即不大于要求值的最大的那个 ...

  5. 苹果手机上input的button按钮颜色显示问题

    在苹果手机上的input按钮自带效果,需要加上outline:0px; -webkit-appearance:none; 清除原有样式,同时苹果手机上的input按钮自带圆角需要按需要去掉 input ...

  6. Python语言中enumerate()及zip()函数的使用例子

    在Python编程语言中,enumerate()及zip()是两个常用的内置函数,这两个函数功能类似,但又有所区别,下面通过两个例子分别进行说明. enumerate()函数 该函数在字面上是枚举.列 ...

  7. AttributeError: module 'datetime' has no attribute 'now'

    在用时间转化时,一直报AttributeError: module 'datetime' has no attribute 'now', 我用的 import datetime   datetime ...

  8. Android 通过应用设置系统日期和时间的方法

    Android 通过应用设置系统日期和时间的方法 android 2.3 android 4.0 测试可行,不过需要ROOT权限. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  9. 两类特殊的Nim游戏:Nim-K游戏与反Nim游戏

    Nim-K游戏 描述 有\(n\)堆石子,每次可从\(k\)堆石子中拿走任意数量的石子. 两个人轮流拿,谁不能拿谁输. 先手必胜条件 把\(n\)堆石子的石子数用二进制表示,统计每一个二进制位上\(1 ...

  10. 20165218 《网络对抗技术》Exp6 信息收集与漏洞扫描

    Exp6 信息收集与漏洞扫描 实践过程记录 一.各种搜索技巧的应用 1_搜索网址目录结构 dir_scanner use auxiliary/scanner/http/dir_scanner This ...