E - Sorted and Sorted


Time limit : 2sec / Memory limit : 1024MB

Score : 600 points

Problem Statement

There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 i 2N) is ai, and the color of this ball is represented by a letter ci. ci = W represents the ball is white; ci = B represents the ball is black.

Takahashi the human wants to achieve the following objective:

  • For every pair of integers (i,j) such that 1 i < j N, the white ball with i written on it is to the left of the white ball with j written on it.
  • For every pair of integers (i,j) such that 1 i < j N, the black ball with i written on it is to the left of the black ball with j written on it.

In order to achieve this, he can perform the following operation:

  • Swap two adjacent balls.

Find the minimum number of operations required to achieve the objective.

Constraints

  • 1 N 2000
  • 1 ai N
  • ci = W or ci = B.
  • If i j, (ai,ci) (aj,cj).

Input

Input is given from Standard Input in the following format:

N
c1 a1
c2 a2
:
c2N a2N

Output

Print the minimum number of operations required to achieve the objective.


Sample Input 1

Copy
3
B 1
W 2
B 3
W 1
W 3
B 2

Sample Output 1

Copy
4

The objective can be achieved in four operations, for example, as follows:

  • Swap the black 3 and white 1.
  • Swap the white 1 and white 2.
  • Swap the black 3 and white 3.
  • Swap the black 3 and black 2.

Sample Input 2

Copy
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1

Sample Output 2

Copy
18

Sample Input 3

Copy
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7

Sample Output 3

Copy
41

https://arc097.contest.atcoder.jp/tasks/arc097_c

dp[i][j]表示前(i + j)个有 i 个白的,j 个黑的,都已经排好序的代价

dpi,j = min(dp[ i − 1 ][ j ] + cost,dp[ i ][ j - 1 ] + cost)

cost是原来位置移到第(i + j)个的代价,即这段中的逆序对个数,树状数组维护即可

 #include<bits/stdc++.h>
typedef long long ll ;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
using namespace std; const int MAXN = ;
const ll INF = 2e9;
int n;
int dp[MAXN][MAXN];
int a[MAXN + MAXN], c[MAXN + MAXN];
int p1[MAXN], p0[MAXN];
int t[MAXN + MAXN]; void add (int k, int d) { while (k <= n + n) { t[k] += d; k += k & -k; } }
int sum (int k) { int s = ; while (k > ) { s += t[k]; k -= k & -k; } return s; } int main() {
cin >> n;
rep(i, , n + n) {
char ch;
cin >> ch >> a[i];
if (ch == 'W') {
p0[a[i]] = i;
c[i] = ;
}
else {
p1[a[i]] = i;
c[i] = ;
}
add(i, );
}
p1[] = n + n + ;
p0[] = n + n + ;
dp[][] = ;
rep(j, , n) {
add(p1[j], -);
dp[][j] = dp[][j - ] + sum(p1[j] - );
}
rep(j, , n) add(p1[j], );
rep(i, , n) {
add(p0[i], -);
rep(j, , n) {
add(p1[j], -);
dp[i][j] = INF;
if (i) dp[i][j] = min(dp[i][j], dp[i - ][j] + sum(p0[i] - ));
if (j) dp[i][j] = min(dp[i][j], dp[i][j - ] + sum(p1[j] - ));
}
rep(j, , n) add(p1[j], );
}
cout << dp[n][n] << "\n";
return ;
}

arc 097 E - Sorted and Sorted的更多相关文章

  1. python中sorted和.sorted 、reversed和reverse的注意点

    L=[1,2,3,4]l1=[123,123,23]if l1.sort() == L.reverse():   #这个判断式是恒等的,因为两个函数的返回值都是None(其实是无返回值)    pri ...

  2. 【AtCoder】 ARC 097

    link C-K-th Substring 题意:找出已知串中第\(k\)大的子串,子串相同的不算 \(k\)好小啊,要怎么做啊 不是[Tjoi2015]弦论吗 算了,直接SAM吧 #include& ...

  3. python中sorted和sorted 、reversed和reverse的使用。

    #encoding = utf-8 list = [1,8,3,6] print(list.sort()) #None print(list) #[1,3,6,8] print(sorted(list ...

  4. AtCoder ARC097C Sorted and Sorted:dp

    传送门 题意 有 $ 2n $ 个球排成一行,其中恰好有 $ n $ 个白球和 $ n $ 个黑球.每个球上写着数字,其中白球上的数字的并集为 $ \lbrace 1 \dots n\rbrace $ ...

  5. ARC097E Sorted and Sorted

    传送门 题目 There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are ...

  6. python 中 sorted() 和 list.sort() 的用法

    今天用python自带的sorted对一个列表进行排序, 在这里总结一下 只要是可迭代对象都可以用sorted . sorted(itrearble, cmp=None, key=None, reve ...

  7. python 排序sorted

    num = [3,2,4,6,5] anum = sorted(num) dnum = sorted(num,reverse=True) print '升序:',anum # 升序: [2, 3, 4 ...

  8. Redis in .NET Core 入门:(5) Sorted SET

    第1篇:https://www.cnblogs.com/cgzl/p/10294175.html 第2篇 String:https://www.cnblogs.com/cgzl/p/10297565. ...

  9. Redis实战 - 2.list、set和Sorted Set

    List Redis的List是通过Linked List(链表)来实现的String集合,所以插入数据的速度很快. 但是缺点就是在数据量比较大的时候,访问某个数据的时间可能会很长,但针对这种情况,可 ...

随机推荐

  1. PTA——求n以内k个质数和

    PTA 7-51 求n以内最大的k个素数以及它们的和 #include<stdio.h> #include<math.h> int isPrime(int n); int ma ...

  2. 解决win7无法运行bat批处理文件的方法

    在win7系统中我们可以将一些命令制作为bat批处理文件,只需双击打开即可运行命令,方便使用. 那么,要怎么运行bat批处理呢?最近有用户反馈,遇到无法运行bat批处理的现象,该怎么办呢? 修复方法一 ...

  3. 20164301 Exp5 MSF基础应用

    Exp5 MSF基础应用 1. 实践内容 1.1一个主动攻击实践,如ms08_067,smb_delivery(唯一) 1.2 一个针对浏览器的攻击,如ms10_046: 1.3 一个针对客户端的攻击 ...

  4. tomcat之jsp连接mysql数据库

    一.下载并部署mysql连接类 首先下载mysql连接类,下载地址https://dev.mysql.com/downloads/connector/j 如图所示,选择第一个箭头所指的平台无关版本,然 ...

  5. 使用kingshard遇到的坑

    禁止用mysqldump 连接kingshard, 会导致表锁死 读取NULL值变为文本 通过kingshard连接 select出来的null值变为文本"NULL" kingsh ...

  6. Flink神秘工具lib

    Flink里面有一个神坑,叫做FI坑.其实只是使用Fi的时候被暴露出来.但是,杀不死你的,终将使你更加强大. Flink集群有一个lib文件件,里面比较happy,可以放各种jar:这样,client ...

  7. python selenium-webdriver 元素操作之鼠标操作(四)

    上节内容主要说明了元素的定位,本节内容说要说对元素的操作,元素的操作分为两部分一部分是鼠标的操作,另一种是对键盘对元素的操作,下面我们主要讲解一下鼠标对元素的操作. webdriver 模块中几种比较 ...

  8. FPGA Asynchronous FIFO设计思路(2)

    FPGA Asynchronous FIFO设计思路(2) 首先讨论格雷码的编码方式: 先看4bit的格雷码,当MSB为0时,正向计数,当MSB为1时,即指针已经走过一遍了,最高位翻转,此时的格雷码是 ...

  9. PHP获取新插入的主键id

    近期在做订单系统开发的时候遇到了此类情景,A表内插入后返回新插入的主键ID,然后用于B表插入数据并携带此id. 目前有几个方法总结 No1.每次插入数据之后返回A表内的最大值,但是对于多用户以及高并发 ...

  10. theano安装问题

    WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain` ...