Submission #2500398


Source Code Expand

Copy
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
template<class V> struct BIT { BIT() {}  // [L, R)
    int NV;vector<V> bit;BIT(int n){init(n);}void init(int n){NV=1;while(NV<n)NV*=2;bit.resize(NV);}
    V operator[](int e) { V s = 0; e++; while (e) s += bit[e - 1], e -= e&-e; return s; }
    void add(int e, V v) { e++; while (e <= NV) bit[e - 1] += v, e += e&-e; }
    int lower_bound(V val) { V tv = 0; int i, ent = 0; for (i = NV - 1; i >= 0; i--)
        if(tv+bit[ent+(1<<i)-1]<=val)tv+=bit[ent+(1<<i)-1],ent += (1 << i);return ent;}
    V get(int L, int R) {assert(0 <= L); assert(R <= NV); assert(L <= R);
        V res = 0; if(R) res += operator[](R - 1); if (L) res -= operator[](L - 1);return res;}
    void clear() { bit.clear(); }};
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/






int N;
char C[4020];
int A[4020];
int smB[2020][4020];
int smW[2020][4020];
int idxB[2020];
int idxW[2020];
//---------------------------------------------------------------------------------------------------
int memo[2020][2020];
int f(int b, int w) {
    if (b == N and w == N) return 0;
    if (0 <= memo[b][w]) return memo[b][w];

    int res = inf;

    if (b != N) {
        /*int cnt = 0;
        rep(i, 0, idxB[b + 1]) {
            if (C[i] == 'B' and b < A[i]) cnt++;
            if (C[i] == 'W' and w < A[i]) cnt++;
        }*/

        int cnt = 0;
        if (0 <= idxB[b + 1] - 1) cnt += smB[b + 1][idxB[b + 1] - 1];
        if (0 <= idxB[b + 1] - 1) cnt += smW[w + 1][idxB[b + 1] - 1];
        
        chmin(res, f(b + 1, w) + cnt);
    }

    if (w != N) {
        /*int cnt = 0;
        rep(i, 0, idxW[w + 1]) {
            if (C[i] == 'B' and b < A[i]) cnt++;
            if (C[i] == 'W' and w < A[i]) cnt++;
        }*/

        int cnt = 0;
        if (0 <= idxW[w + 1] - 1) cnt += smB[b + 1][idxW[w + 1] - 1];
        if (0 <= idxW[w + 1] - 1) cnt += smW[w + 1][idxW[w + 1] - 1];
        chmin(res, f(b, w + 1) + cnt);
    }

    return memo[b][w] = res;
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 0, 2 * N) cin >> C[i] >> A[i];

    rep(i, 0, 2 * N) {
        if (C[i] == 'B') smB[A[i]][i]++;
        else smW[A[i]][i]++;
    }
    rep(i, 0, N + 5) {
        rep(j, 1, 2 * N) smB[i][j] += smB[i][j - 1];
        rep(j, 1, 2 * N) smW[i][j] += smW[i][j - 1];
    }
    rep(j, 0, 2 * N) {
        rrep(i, N+4, 0) smB[i][j] += smB[i + 1][j];
        rrep(i, N+4, 0) smW[i][j] += smW[i + 1][j];
    }

    rep(i, 0, 2 * N) {
        if (C[i] == 'B') idxB[A[i]] = i;
        if (C[i] == 'W') idxW[A[i]] = i;
    }

    rep(i, 0, N + 1) rep(j, 0, N + 1) memo[i][j] = -1;
    cout << f(0, 0) << endl;
}

Submission Info

Submission Time
Task E - Sorted and Sorted
User hamayanhamayan
Language C++14 (GCC 5.4.1)
Score 600
Code Size 3948 Byte
Status AC
Exec Time 202 ms
Memory 79616 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 36
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 155 ms 74112 KB
1_015.txt AC 20 ms 32768 KB
1_016.txt AC 106 ms 65920 KB
1_017.txt AC 39 ms 43392 KB
1_018.txt AC 3 ms 8960 KB
1_019.txt AC 15 ms 28544 KB
1_020.txt AC 119 ms 70016 KB
1_021.txt AC 39 ms 45440 KB
1_022.txt AC 21 ms 34944 KB
1_023.txt AC 83 ms 65920 KB
1_024.txt AC 151 ms 78976 KB
1_025.txt AC 192 ms 79616 KB
1_026.txt AC 198 ms 79616 KB
1_027.txt AC 202 ms 79616 KB
1_028.txt AC 199 ms 79616 KB
1_029.txt AC 198 ms 79616 KB
1_030.txt AC 200 ms 79616 KB
1_031.txt AC 196 ms 79616 KB
1_032.txt AC 133 ms 79616 KB
1_033.txt AC 143 ms 79616 KB
1_034.txt AC 136 ms 79616 KB
1_035.txt AC 156 ms 79616 KB