Submission #3924134
Source Code Expand
#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; #define REP(i,n) for (ll i = 0; i < n; ++i) #define FOR(i,m,n) for (ll i = m; i < n; ++i) struct BIT { vector<ll> data; int N; BIT(int n) { N = n; data = vector<ll>(N+1); } void add(ll i, ll x) { for (ll k = i; k <= N; k += k&-k) data[k] += x; } void update(int i, ll x) { add(i, x-get(i)); } ll sum(int i) { ll res = 0; for (int k = i; k > 0; k -= k&-k) res += data[k]; return res; } ll get(int i) { return sum(i) - sum(i-1); } }; const int MAX = 2019; ll N; string s; int a; ll dp[MAX][MAX]; //dp[i][j]:左から黒をi個、白をj個並べるためにかかった回数 int b[MAX], w[MAX], x[MAX*2]; int main() { cin >> N; REP (i, 2*N) { cin >> s >> a; if (s == "B") { x[i] = a; b[a] = i; } else { x[i] = a+N; w[a] = i; } } dp[0][0] = 0; BIT bitBlack(N*2); BIT bitWhite(N*2); FOR (i, 1, N+1) { bitBlack.add(b[i]+1, 1); dp[i][0] = dp[i-1][0] + b[i] - bitBlack.sum(b[i]+1) + 1; bitWhite.add(w[i]+1, 1); dp[0][i] = dp[0][i-1] + w[i] - bitWhite.sum(w[i]+1) + 1; } bitBlack = BIT(N*2); FOR (i, 1, N+1) { bitBlack.add(b[i]+1, 1); bitWhite = BIT(N*2); FOR (j, 1, N+1) { bitWhite.add(w[j]+1, 1); int new_b = dp[i-1][j] + b[i] - bitBlack.sum(b[i]+1) - bitWhite.sum(b[i]+1) + 1; int new_w = dp[i][j-1] + w[j] - bitBlack.sum(w[j]+1) - bitWhite.sum(w[j]+1) + 1; dp[i][j] = min(new_b, new_w); } } cout << dp[N][N] << endl; }
Submission Info
Submission Time | |
---|---|
Task | E - Sorted and Sorted |
User | tnyo43 |
Language | C++14 (GCC 5.4.1) |
Score | 600 |
Code Size | 1691 Byte |
Status | AC |
Exec Time | 170 ms |
Memory | 32000 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 600 / 600 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | 0_000.txt, 0_001.txt, 0_002.txt |
All | 0_000.txt, 0_001.txt, 0_002.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 1_033.txt, 1_034.txt, 1_035.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_000.txt | AC | 1 ms | 256 KB |
0_001.txt | AC | 1 ms | 256 KB |
0_002.txt | AC | 1 ms | 256 KB |
1_003.txt | AC | 1 ms | 256 KB |
1_004.txt | AC | 1 ms | 256 KB |
1_005.txt | AC | 1 ms | 256 KB |
1_006.txt | AC | 1 ms | 256 KB |
1_007.txt | AC | 1 ms | 256 KB |
1_008.txt | AC | 1 ms | 256 KB |
1_009.txt | AC | 1 ms | 256 KB |
1_010.txt | AC | 1 ms | 256 KB |
1_011.txt | AC | 1 ms | 256 KB |
1_012.txt | AC | 1 ms | 256 KB |
1_013.txt | AC | 1 ms | 256 KB |
1_014.txt | AC | 133 ms | 28928 KB |
1_015.txt | AC | 21 ms | 11776 KB |
1_016.txt | AC | 99 ms | 24832 KB |
1_017.txt | AC | 41 ms | 16128 KB |
1_018.txt | AC | 2 ms | 768 KB |
1_019.txt | AC | 15 ms | 9600 KB |
1_020.txt | AC | 115 ms | 26880 KB |
1_021.txt | AC | 31 ms | 16256 KB |
1_022.txt | AC | 17 ms | 11776 KB |
1_023.txt | AC | 66 ms | 24832 KB |
1_024.txt | AC | 116 ms | 31360 KB |
1_025.txt | AC | 170 ms | 31872 KB |
1_026.txt | AC | 170 ms | 31872 KB |
1_027.txt | AC | 170 ms | 31872 KB |
1_028.txt | AC | 169 ms | 32000 KB |
1_029.txt | AC | 169 ms | 31872 KB |
1_030.txt | AC | 169 ms | 31872 KB |
1_031.txt | AC | 169 ms | 32000 KB |
1_032.txt | AC | 107 ms | 31872 KB |
1_033.txt | AC | 115 ms | 32000 KB |
1_034.txt | AC | 108 ms | 31872 KB |
1_035.txt | AC | 113 ms | 31872 KB |