Submission #3000011


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;

#define NDEBUG
#ifdef DEBUG
#include "../cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))
#define cons make_pair


ll solve(int N,vi& c,vi& a) {
    int W = N*2;

    vi pB(N+1), pW(N+1); // pB[i] = 黒iの位置
    rep(i,W){
        if (c[i]) {
            pB[a[i]] = i; // black
        } else {
            pW[a[i]] = i; // white
        }
    }
#ifdef DEBUG
    cerr << "pB[]: " << pB << endl;
    cerr << "pW[]: " << pW << endl;
#endif

    // vvi cB(N+1, vi(W+1, 0)), cW(N+1, vi(W+1, 0));
    vvi cB(N+2), cW(N+2);
    cB[N+1] = vi(W+1, 0);
    cW[N+1] = vi(W+1, 0);
    // vvi cB(W+1, vi(N+1, 0)), cW(W+1, vi(N+1, 0));
    // cX[j][k] : 左からk個[0..W]のうち、j以上の白(ないし黒)の個数
    for (int j=N; j>=1; --j) {
        {
            int here = pB[j];
            cB[j] = cB[j+1];
            for (int i=here+1; i<=W; ++i) ++cB[j][i];
        }
        {
            int here = pW[j];
            cW[j] = cW[j+1];
            for (int i=here+1; i<=W; ++i) ++cW[j][i];
        }
    }
    cB[0] = cB[1];
    cW[0] = cW[1];

#ifdef DEBUG
    fprintf(stderr, "cB[][]:\n");
    rep(i, N+1) {
        rep(k, W+1) {
            fprintf(stderr, " %2d", cB[i][k]);
        }
        fprintf(stderr, "\n");
    }
    fprintf(stderr, "cW[][]:\n");
    rep(i, N+1) {
        rep(k, W+1) {
            fprintf(stderr, " %2d", cW[i][k]);
        }
        fprintf(stderr, "\n");
    }
#endif
    vvi dp(N+1, vi(N+1, INT_MAX));
    dp[0][0] = 0; // dp]黒いくつまで][白いくつまで]
    for (int j=1; j<=N; ++j) {
        // 黒0 白j
        int here = pW[j];
        int f = cW[j+1][here] + cB[1][here];
        dp[0][j] = dp[0][j-1] + f;
    }
    for (int i=1; i<=N; ++i) {
        // 黒i 白0
        int here = pB[i];
        int f = cB[i+1][here] + cW[1][here];
        dp[i][0] = dp[i-1][0] + f;
    }

    for (int i=1; i<=N; ++i) {
        for (int j=1; j<=N; ++j) {
            int costB, costW;
            {
                // 白はj固定、黒を i-1 -> i
                int here = pB[i];
                costB = cB[i+1][here] + cW[j+1][here];
            }
            {
                // 黒はi固定、白を j-1 -> j
                int here = pW[j];
                costW = cB[i+1][here] + cW[j+1][here];
            }
            dp[i][j] = min(dp[i-1][j] + costB, dp[i][j-1] + costW);
        }
    }
#ifdef DEBUG
    fprintf(stderr, "dp[i][j]:\n");
    rep(i, N+1) {
        rep(j, N+1) {
            fprintf(stderr, " %2d", dp[i][j]);
        }
        fprintf(stderr, "\n");
    }
#endif

    return dp[N][N];
}

int main() {
    int N; cin >> N;
    vi c(N*2), a(N*2);
    rep(i, N*2){
        char ch; int ai; cin >> ch >> ai;
        c[i] = (ch == 'B');
        a[i] = ai; // - 1;
    }
    cout << solve(N,c,a) << endl;
    return 0;
}

Submission Info

Submission Time
Task E - Sorted and Sorted
User naoya_t
Language C++14 (GCC 5.4.1)
Score 600
Code Size 4009 Byte
Status
Exec Time 178 ms
Memory 78720 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 0_000.txt, 0_001.txt, 0_002.txt
All 600 / 600 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 1 ms 256 KB
0_001.txt 1 ms 256 KB
0_002.txt 1 ms 256 KB
1_003.txt 1 ms 256 KB
1_004.txt 1 ms 256 KB
1_005.txt 1 ms 256 KB
1_006.txt 1 ms 256 KB
1_007.txt 1 ms 256 KB
1_008.txt 1 ms 256 KB
1_009.txt 1 ms 256 KB
1_010.txt 1 ms 256 KB
1_011.txt 1 ms 256 KB
1_012.txt 1 ms 256 KB
1_013.txt 1 ms 256 KB
1_014.txt 131 ms 61568 KB
1_015.txt 16 ms 9216 KB
1_016.txt 97 ms 45824 KB
1_017.txt 34 ms 19072 KB
1_018.txt 1 ms 512 KB
1_019.txt 11 ms 6400 KB
1_020.txt 114 ms 53632 KB
1_021.txt 41 ms 21120 KB
1_022.txt 20 ms 11136 KB
1_023.txt 93 ms 46336 KB
1_024.txt 162 ms 75776 KB
1_025.txt 174 ms 78720 KB
1_026.txt 175 ms 78720 KB
1_027.txt 174 ms 78720 KB
1_028.txt 174 ms 78720 KB
1_029.txt 174 ms 78720 KB
1_030.txt 174 ms 78720 KB
1_031.txt 174 ms 78720 KB
1_032.txt 178 ms 78720 KB
1_033.txt 176 ms 78720 KB
1_034.txt 168 ms 78720 KB
1_035.txt 166 ms 78720 KB