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:

  1. N
  2. c1 a1
  3. c2 a2
  4. :
  5. c2N a2N

Output

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


Sample Input 1

Copy
  1. 3
  2. B 1
  3. W 2
  4. B 3
  5. W 1
  6. W 3
  7. B 2

Sample Output 1

Copy
  1. 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
  1. 4
  2. B 4
  3. W 4
  4. B 3
  5. W 3
  6. B 2
  7. W 2
  8. B 1
  9. W 1

Sample Output 2

Copy
  1. 18

Sample Input 3

Copy
  1. 9
  2. W 3
  3. B 1
  4. B 4
  5. W 1
  6. B 5
  7. W 9
  8. W 2
  9. B 6
  10. W 5
  11. B 3
  12. W 8
  13. B 9
  14. W 7
  15. B 2
  16. B 8
  17. W 4
  18. W 6
  19. B 7

Sample Output 3

Copy
  1. 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)个的代价,即这段中的逆序对个数,树状数组维护即可

  1. #include<bits/stdc++.h>
  2. typedef long long ll ;
  3. #define rep(i, a, b) for (int i = a; i <= b; ++i)
  4. using namespace std;
  5.  
  6. const int MAXN = ;
  7. const ll INF = 2e9;
  8. int n;
  9. int dp[MAXN][MAXN];
  10. int a[MAXN + MAXN], c[MAXN + MAXN];
  11. int p1[MAXN], p0[MAXN];
  12. int t[MAXN + MAXN];
  13.  
  14. void add (int k, int d) { while (k <= n + n) { t[k] += d; k += k & -k; } }
  15. int sum (int k) { int s = ; while (k > ) { s += t[k]; k -= k & -k; } return s; }
  16.  
  17. int main() {
  18. cin >> n;
  19. rep(i, , n + n) {
  20. char ch;
  21. cin >> ch >> a[i];
  22. if (ch == 'W') {
  23. p0[a[i]] = i;
  24. c[i] = ;
  25. }
  26. else {
  27. p1[a[i]] = i;
  28. c[i] = ;
  29. }
  30. add(i, );
  31. }
  32. p1[] = n + n + ;
  33. p0[] = n + n + ;
  34. dp[][] = ;
  35. rep(j, , n) {
  36. add(p1[j], -);
  37. dp[][j] = dp[][j - ] + sum(p1[j] - );
  38. }
  39. rep(j, , n) add(p1[j], );
  40. rep(i, , n) {
  41. add(p0[i], -);
  42. rep(j, , n) {
  43. add(p1[j], -);
  44. dp[i][j] = INF;
  45. if (i) dp[i][j] = min(dp[i][j], dp[i - ][j] + sum(p0[i] - ));
  46. if (j) dp[i][j] = min(dp[i][j], dp[i][j - ] + sum(p1[j] - ));
  47. }
  48. rep(j, , n) add(p1[j], );
  49. }
  50. cout << dp[n][n] << "\n";
  51. return ;
  52. }

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. 在Vuex使用 以及 dispatch和commit来调用mutations的区别

    main.js中 import Vuex from 'vuex' Vue.use(vuex); const store = new Vuex.store({ state: { nickName: &q ...

  2. OnTriggerEnter2D方法

    我两个物体A,B都添加了Circle Collider 2D,并且都勾选了is Trigger,我在A的脚本里用void OnTriggerEnter2D(Collider2D coll)检测碰撞,至 ...

  3. hdu 4506 快速幂

    小明自从告别了ACM/ICPC之后,就开始潜心研究数学问题了,一则可以为接下来的考研做准备,再者可以借此机会帮助一些同学,尤其是漂亮的师妹.这不,班里唯一的女生又拿一道数学题来请教小明,小明当然很高兴 ...

  4. C# DES加密类,16位的加密。

    这个加密类是与java写的DES加密不同时,自己写的,最后与Java的加密相同了,解决了加密后不同的问题. 可以直接调用里面的加密和解密的方法. using System; using System. ...

  5. 第五章HTML

    HTML介绍 标签:有一个头,一尾 <!DOCTYPE html><html lang="en"><head> <!-- 文档的标题.编码 ...

  6. py2x与py3x区别

    https://blog.csdn.net/samxx8/article/details/21535901

  7. jenkins如何获取gitlab上的代码

    如何安装jenkins和gitlab我就不重复了,请自行搜索我的博客 那么,jenkins如何获取gitlab上的代码呢? 具体配置步骤如下 1.在gitlab上配置个人访问令牌.注意事项:姓名那里需 ...

  8. Maven 新手入门+命令大全

    Maven 是一个项目管理工具,可以对 Java 项目进行构建.依赖管理. Maven 官方文档(English): http://maven.apache.org/index.html Maven ...

  9. PHP/Post 提交请求获取json数据,并转化为所需要的数组

    /** * Post 提交请求获取json数据,并转化为所需要的数组 */ function request_post($url = '', $param = '') { if (empty($url ...

  10. CRM-展示列表,分页功能

    目录 一.admin (创建超级用户) 二.展示列表 三.分页(封装成类)   一.admin (创建超级用户) 1.注册: 1.创建一个超级管理员,使用如下命令: python manage.py ...