Submission #4016762
Source Code Expand
#include <iostream> #include <vector> #include <map> #include <unordered_map> #include <queue> #include <set> #include <algorithm> #include <string> #include <math.h> #include <limits.h> #include <stack> #include <complex> using namespace std; typedef long long ll; typedef pair<ll, ll> P; int N; vector<P> ball; int b[2100]; //それぞれの番号が何番目にあるか int w[2100]; int w_offset[2100][2100]; int b_offset[2100][2100]; int dp[2100][2100]; int main(){ cin >> N; for(int i = 0; i < 2*N; i++){ char c; cin >> c; int a; cin >> a; if(c == 'W'){ w[a] = i+1; ball.emplace_back(P(0, a)); } else{ b[a] = i+1; ball.emplace_back(P(1, a)); } } for(int i = 0; i <= N; i++){ fill(w_offset[i], w_offset[i]+N+10, 0); fill(b_offset[i], b_offset[i]+N+10, 0); fill(dp[i], dp[i]+N+10, 0); } for(int i = 1; i <= N; i++) { int w_plus = 0; int b_plus = 0; for (int j = i - 1; j > 0; j--) { if (w[i] < w[j]) { w_plus++; } if (b[i] < b[j]) { b_plus++; } } w_offset[0][i] = w_plus; b_offset[0][i] = b_plus; } // bがiだけ進んだ時にwがどれくらいずれるか for(int i = 1; i <= N; i++){ int b_pos = b[i]; for(int j = 1; j <= N; j++){ if(b_pos > w[j]){ w_offset[i][j] = w_offset[i-1][j] + 1; } else{ w_offset[i][j] = w_offset[i-1][j]; } } } // wがiだけ進んだ時にbがどれくらいずれるか for(int i = 1; i <= N; i++){ int w_pos = w[i]; for(int j = 1; j <= N; j++){ if(w_pos > b[j]){ b_offset[i][j] = b_offset[i-1][j] + 1; } else{ b_offset[i][j] = b_offset[i-1][j]; } } } for(int w_i = 1; w_i <= N; w_i++){ int move = w[w_i] + w_offset[0][w_i] - w_i; // cout << w_i << " " << w_offset[0][w_i] << " " << move << endl; dp[w_i][0] = dp[w_i-1][0] + move; } for(int b_i = 1; b_i <= N; b_i++){ int move = b[b_i] + b_offset[0][b_i] - b_i; // cout << b_i << " " << w_offset[0][b_i] << " " << move << endl; dp[0][b_i] = dp[0][b_i-1] + move; } for(int b_i = 1; b_i <= N; b_i++){ for(int w_i = 1; w_i <= N; w_i++){ //w_i+b_i番目に置く。 int w_move = w[w_i] + w_offset[b_i][w_i] - (w_i+b_i); //wを置くとき。 int b_move = b[b_i] + b_offset[w_i][b_i] - (w_i+b_i); //bを置くとき。 dp[w_i][b_i] = min(dp[w_i-1][b_i] + w_move, dp[w_i][b_i-1] + b_move); // cout << dp[w_i][b_i] << " "; } // cout << endl; } cout << dp[N][N] << endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Sorted and Sorted |
User | bomac1 |
Language | C++14 (GCC 5.4.1) |
Score | 600 |
Code Size | 3065 Byte |
Status | AC |
Exec Time | 65 ms |
Memory | 51584 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 | 2 ms | 4352 KB |
0_001.txt | AC | 2 ms | 4352 KB |
0_002.txt | AC | 2 ms | 4352 KB |
1_003.txt | AC | 2 ms | 4352 KB |
1_004.txt | AC | 2 ms | 4352 KB |
1_005.txt | AC | 2 ms | 4352 KB |
1_006.txt | AC | 2 ms | 4352 KB |
1_007.txt | AC | 2 ms | 4352 KB |
1_008.txt | AC | 2 ms | 4352 KB |
1_009.txt | AC | 2 ms | 4352 KB |
1_010.txt | AC | 2 ms | 4352 KB |
1_011.txt | AC | 2 ms | 4352 KB |
1_012.txt | AC | 2 ms | 4352 KB |
1_013.txt | AC | 2 ms | 4352 KB |
1_014.txt | AC | 51 ms | 49536 KB |
1_015.txt | AC | 11 ms | 22400 KB |
1_016.txt | AC | 37 ms | 43392 KB |
1_017.txt | AC | 21 ms | 28928 KB |
1_018.txt | AC | 3 ms | 6912 KB |
1_019.txt | AC | 9 ms | 20352 KB |
1_020.txt | AC | 45 ms | 47488 KB |
1_021.txt | AC | 16 ms | 30976 KB |
1_022.txt | AC | 10 ms | 22528 KB |
1_023.txt | AC | 29 ms | 43392 KB |
1_024.txt | AC | 48 ms | 51584 KB |
1_025.txt | AC | 63 ms | 51584 KB |
1_026.txt | AC | 64 ms | 51584 KB |
1_027.txt | AC | 64 ms | 51584 KB |
1_028.txt | AC | 64 ms | 51584 KB |
1_029.txt | AC | 64 ms | 51584 KB |
1_030.txt | AC | 65 ms | 51584 KB |
1_031.txt | AC | 64 ms | 51584 KB |
1_032.txt | AC | 49 ms | 51584 KB |
1_033.txt | AC | 50 ms | 51584 KB |
1_034.txt | AC | 52 ms | 51584 KB |
1_035.txt | AC | 50 ms | 51584 KB |