^m
路漫漫

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

 

posted on 2018-06-03 20:28  ^m  阅读(155)  评论(0编辑  收藏  举报