Submission #3934513
Source Code Expand
#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define int long long
#define INF 1000000000000000000
using namespace std;
#define ANS(f) if(f) cout << "YES" << endl; else cout << "NO" << endl;
typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;
template<typename T>
void readv(vector<T> &a){ REP(i, a.size()) cin >> a[i]; }
void readi(vector<int> &a){ REP(i, a.size()){cin >> a[i]; a[i]--;} }
void debug(mat m){REP(i, m.size()){ REP(j, m[i].size()){ cout << m[i][j] << ","; } cout << endl; }}
signed main(){
int N; cin >> N;
vector<char> c(2 * N);
vec a(2 * N);
REP(i, 2 * N){
cin >> c[i] >> a[i];
a[i]--;
}
mat inv(2, vec(N));
REP(i, 2 * N){
if(c[i] == 'W') inv[0][a[i]] = i;
else inv[1][a[i]] = i;
}
mat B(N + 1, vec(2 * N + 1, 0)), W(N + 1, vec(2 * N + 1, 0));
REP(i, N) FOR(j, inv[0][i] + 1, 2 * N + 1) W[i + 1][j] = 1;
REP(j, 2 * N + 1) FOR(i, 1, N + 1) W[i][j] += W[i - 1][j];
REP(i, N) FOR(j, inv[1][i] + 1, 2 * N + 1) B[i + 1][j] = 1;
REP(j, 2 * N + 1) FOR(i, 1, N + 1) B[i][j] += B[i - 1][j];
mat dp(2 * N + 1, vec(N + 1, INF));
dp[0][0] = 0;
FOR(i, 1, 2 * N + 1){
REP(j, min(i, N) + 1){
int v1 = INF, v2 = INF;
int nW = i - j, nB = j;
if(nW > N) continue;
//W置いた
if(nW >= 1) v1 = dp[i - 1][j] + nW - 1 + nB - W[nW - 1][inv[0][nW - 1]] - B[nB][inv[0][nW - 1]];
//B置いた
if(nB >= 1) v2 = dp[i - 1][j - 1] + nB - 1 + nW - B[nB - 1][inv[1][nB - 1]] - W[nW][inv[1][nB - 1]];
dp[i][j] = min(v1, v2);
}
}
cout << dp[2 * N][N] << endl;
return 0;
}
Submission Info
Submission Time |
|
Task |
E - Sorted and Sorted |
User |
sumitacchan |
Language |
C++14 (GCC 5.4.1) |
Score |
600 |
Code Size |
2197 Byte |
Status |
AC |
Exec Time |
646 ms |
Memory |
188288 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 |
484 ms |
147200 KB |
1_015.txt |
AC |
55 ms |
21760 KB |
1_016.txt |
AC |
346 ms |
109440 KB |
1_017.txt |
AC |
127 ms |
45184 KB |
1_018.txt |
AC |
2 ms |
768 KB |
1_019.txt |
AC |
32 ms |
15104 KB |
1_020.txt |
AC |
411 ms |
128000 KB |
1_021.txt |
AC |
144 ms |
50176 KB |
1_022.txt |
AC |
69 ms |
26240 KB |
1_023.txt |
AC |
342 ms |
110464 KB |
1_024.txt |
AC |
606 ms |
181120 KB |
1_025.txt |
AC |
641 ms |
188288 KB |
1_026.txt |
AC |
643 ms |
188288 KB |
1_027.txt |
AC |
646 ms |
188288 KB |
1_028.txt |
AC |
640 ms |
188288 KB |
1_029.txt |
AC |
643 ms |
188288 KB |
1_030.txt |
AC |
642 ms |
188288 KB |
1_031.txt |
AC |
641 ms |
188288 KB |
1_032.txt |
AC |
644 ms |
188288 KB |
1_033.txt |
AC |
643 ms |
188288 KB |
1_034.txt |
AC |
634 ms |
188288 KB |
1_035.txt |
AC |
635 ms |
188288 KB |