arc 097 E - Sorted and Sorted

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

 

 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 = 2333;
 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 = 0; while (k > 0) { s += t[k]; k -= k & -k; } return s; }
16 
17 int main() {
18     cin >> n;
19     rep(i, 1, n + n) {
20         char ch;
21         cin >> ch >> a[i];
22         if (ch == 'W') {
23             p0[a[i]] = i;
24             c[i] = 0;
25         }
26         else {
27             p1[a[i]] = i;
28             c[i] = 1;
29         }
30         add(i, 1);
31     }
32     p1[0] = n + n + 1;
33     p0[0] = n + n + 1;
34     dp[0][0] = 0;
35     rep(j, 1, n) {
36         add(p1[j], -1);
37         dp[0][j] = dp[0][j - 1] + sum(p1[j] - 1);
38     }
39     rep(j, 0, n) add(p1[j], 1);
40     rep(i, 1, n) {
41         add(p0[i], -1);
42         rep(j, 0, n) {
43             add(p1[j], -1);
44             dp[i][j] = INF;
45             if (i) dp[i][j] = min(dp[i][j], dp[i - 1][j] + sum(p0[i] - 1));
46             if (j) dp[i][j] = min(dp[i][j], dp[i][j - 1] + sum(p1[j] - 1));
47         }
48         rep(j, 0, n) add(p1[j], 1);
49     }
50     cout << dp[n][n] << "\n";
51     return 0;
52 } 
View Code

 

转载于:https://www.cnblogs.com/m-m-m/p/9130311.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
邀请码:a54ed48fee85400295d14c9e6def970b “奇奇SEO优化”钱柜软件是米图团队经过对搜素引擎算法多年跟踪研究,采用全新的智能云优化技术,开发出来的一款搜索引擎关键词优化工具:奇奇SEO优化软件。奇奇SEO优化软件能迅速在百度、谷歌、搜狗、搜搜、雅虎、有道、必应等搜索引擎中,依次进行目标网站的搜索,大幅度提高搜索引擎对网站的注重程度和关注度,提高网站在搜索引擎中的权重值。提高搜索引擎收录频率和收录量;提高网站访问量和关键字位置排名,迅速将优化的关键词显示在底层相关搜索,同样可以智能优化搜索引擎搜索下拉框,快速占领最火爆最抢眼的十个搜索下拉位置。我们坚信:必定成为网站必备工具! 奇奇SEO优化软件亮点 ●点击逼真。和真人点击一样,众多挂机者自动点击,分布式点击,挂机环境各不相同,点击逼真、真实,效果看得见! ●支持所有主流搜索引擎。挂机时会看到,全面、功能强大! ●支持点击地区设置。有针对性点击,和搜索引擎中的设置对应,针对性强,避免空点,提高效率! ●清空浏览痕迹、cookies、清除浏览缓存,点击有效 ●目标网页随机停留数秒后自动关闭 ●目标网页随机位置、随机二次点击、深入点击,将真实演绎到底! ●免费。挂机有积分即可免费使用,无资金担忧。 ●批量删除、批量更新、批量启用、批量停止 简单易操作 您可以用第一名点击器,来做这些事情 ●点击各类搜索引擎的竞价广告 ●点击关键词在搜索引擎结果中的自己的网站,提高关键词搜索排名 ●提高网站排名、提高关键词搜索指数、百度指数 ●刷百度下拉框、刷相关搜索 ●点击淘宝直通车竞价广告 ●点击阿里网销宝竞价广告任何你想象和在最新软件版本功能基础上,你可能用到第一名点击器。上述提及的搜索引擎,囊括了百度、谷歌、搜搜、搜狗、必应、有道、淘宝直通车、阿里网销宝等所有常见的搜素引擎,并且在以后使用中,视网友使用频率建议会不断增加,比如单百度点击,我们就已经包括了百度网页、百度图片、百度知道等搜索类型的点击,第一名点击器功能之强大,真正拿出时间好好用过的人,最有体会!当然,众多的功能,对绝大多数网友可能只使用了冰山一角,更多的潜在的用武之地,待你挖掘和琢磨! 奇奇SEO优化软件特点 1、简单易用 使用简单,操作方便,自动升级,针对个人网站永久免费。 2、高度安全 基于沙盒原理,有效拦截各类病毒木马下载程序,自动、收集过滤恶意网址。 3、真实有效 基于智能云优化技术,利用网络节点地域差异性,用户随机性,使访问效果真实有效 4、优化原理及手法 通过反馈网站权重数据包给搜索引擎,合理的分配网站的权重,提升网站的关注度和友好度,从而达到网站关键字排名迅速提升的效果。 5、完全兼容虚拟机运行

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值