Submission #2502904
Source Code Expand
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define forn(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define ford(i, a, n) for (int i = (int)(n) - 1; i >= (int)(a); --i)
#define fore(i, a, n) for (int i = (int)(a); i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define fs first
#define sn second
#define trace(a)\
for (auto i : a) cerr << i << ' ';\
cerr << '\n'
#define eb emplace_back
#ifndef M_PI
const ld M_PI = acos(-1.0);
#endif
template<typename T>
inline void setmax(T& a, T b) {
if (a < b) a = b;
}
template<typename T>
inline void setmin(T& a, T b) {
if (a > b) a = b;
}
template<typename T, typename S>
istream& operator>> (istream& in, pair<S, T>& p) {
in >> p.fs >> p.sn;
return in;
}
template<typename T, typename S>
ostream& operator<< (ostream& out, pair<S, T>& p) {
out << p.fs << ' ' << p.sn << ' ';
return out;
}
template<typename T>
istream& operator>> (istream& in, vector<T>& v) {
for (T& x : v) in >> x;
return in;
}
template<typename T>
ostream& operator<< (ostream& out, vector<T>& v) {
for (T& x : v) out << x << ' ';
return out;
}
const ld eps = 1e-9;
const int INF = 2000000000;
const ll LINF = 1ll * INF * INF;
const ll MOD = 1000000007;
const int N = 2018;
int w[N][2 * N], b[N][2 * N];
int posw[N], posb[N];
int dp[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
srand((unsigned)chrono::high_resolution_clock::now().time_since_epoch().count());
int n;
cin >> n;
forn(i, 0, 2 * n) {
char c;
int val;
cin >> c >> val;
(c == 'B' ? posb : posw)[--val] = i;
}
forn(i, 0, n) {
if (i) {
forn(j, 0, 2 * n) w[i][j] = w[i - 1][j];
forn(j, 0, 2 * n) b[i][j] = b[i - 1][j];
}
forn(j, posw[i], 2 * n) ++w[i][j];
forn(j, posb[i], 2 * n) ++b[i][j];
}
fore(i, 0, n)
fore(j, 0, n) {
if (i == 0 && j == 0) continue;
dp[i][j] = INF;
if (i) {
--i, --j;
int lftI = posw[i] - (i ? w[i - 1][posw[i]] : 0) - (j == -1 ? 0 : b[j][posw[i]]);
++i, ++j;
setmin(dp[i][j], dp[i - 1][j] + lftI);
}
if (j) {
--i, --j;
int lftJ = posb[j] - (j ? b[j - 1][posb[j]] : 0) - (i == -1 ? 0 : w[i][posb[j]]);
++i, ++j;
setmin(dp[i][j], dp[i][j - 1] + lftJ);
}
}
//fore(i, 0, n) {
// fore(j, 0, n) cerr << dp[i][j] << ' ';
// cerr << '\n';
//}
cout << dp[n][n] << '\n';
}
Submission Info
| Submission Time |
|
| Task |
E - Sorted and Sorted |
| User |
khadaev |
| Language |
C++14 (GCC 5.4.1) |
| Score |
600 |
| Code Size |
2941 Byte |
| Status |
AC |
| Exec Time |
111 ms |
| Memory |
79488 KiB |
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 KiB |
| 0_001.txt |
AC |
2 ms |
4352 KiB |
| 0_002.txt |
AC |
2 ms |
4352 KiB |
| 1_003.txt |
AC |
2 ms |
4352 KiB |
| 1_004.txt |
AC |
2 ms |
4352 KiB |
| 1_005.txt |
AC |
2 ms |
4352 KiB |
| 1_006.txt |
AC |
2 ms |
4352 KiB |
| 1_007.txt |
AC |
2 ms |
4352 KiB |
| 1_008.txt |
AC |
2 ms |
4352 KiB |
| 1_009.txt |
AC |
2 ms |
4352 KiB |
| 1_010.txt |
AC |
2 ms |
4352 KiB |
| 1_011.txt |
AC |
2 ms |
4352 KiB |
| 1_012.txt |
AC |
2 ms |
4352 KiB |
| 1_013.txt |
AC |
2 ms |
4352 KiB |
| 1_014.txt |
AC |
81 ms |
73984 KiB |
| 1_015.txt |
AC |
13 ms |
30592 KiB |
| 1_016.txt |
AC |
61 ms |
65792 KiB |
| 1_017.txt |
AC |
23 ms |
43264 KiB |
| 1_018.txt |
AC |
3 ms |
8832 KiB |
| 1_019.txt |
AC |
11 ms |
28544 KiB |
| 1_020.txt |
AC |
76 ms |
71936 KiB |
| 1_021.txt |
AC |
22 ms |
45312 KiB |
| 1_022.txt |
AC |
13 ms |
34816 KiB |
| 1_023.txt |
AC |
52 ms |
65792 KiB |
| 1_024.txt |
AC |
66 ms |
78976 KiB |
| 1_025.txt |
AC |
110 ms |
79488 KiB |
| 1_026.txt |
AC |
108 ms |
79488 KiB |
| 1_027.txt |
AC |
110 ms |
79488 KiB |
| 1_028.txt |
AC |
106 ms |
79488 KiB |
| 1_029.txt |
AC |
111 ms |
79488 KiB |
| 1_030.txt |
AC |
108 ms |
79488 KiB |
| 1_031.txt |
AC |
111 ms |
79488 KiB |
| 1_032.txt |
AC |
69 ms |
79488 KiB |
| 1_033.txt |
AC |
61 ms |
79488 KiB |
| 1_034.txt |
AC |
86 ms |
79488 KiB |
| 1_035.txt |
AC |
67 ms |
79488 KiB |