Submission #8327136
Source Code Expand
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; const int INF=1<<29; template<class G> class Fenwick_tree{ vector<G> a; public: Fenwick_tree(int n):a(n){} void add(int i,G val){ for(;i<a.size();i|=i+1) a[i]+=val; } G sum(int l,int r){ if(l==0){ G res{}; for(r--;r>=0;r=(r&(r+1))-1) res+=a[r]; return res; } return sum(0,r)-sum(0,l); } }; int main(){ int n; cin>>n; int pos[2][2000]; rep(i,2*n){ char c; int a; cin>>c>>a; a--; if(c=='W') pos[0][a]=i; else pos[1][a]=i; } static int dp[2001][2001]; rep(i,n+1) rep(j,n+1) dp[i][j]=INF; dp[0][0]=0; Fenwick_tree<int> F0(2*n); rep(i,n+1){ Fenwick_tree<int> F1(2*n); rep(j,n+1){ if(i<n){ // append (white, i) int x=pos[0][i]; int cost=x-F0.sum(0,x)-F1.sum(0,x); dp[i+1][j]=min(dp[i+1][j],dp[i][j]+cost); } if(j<n){ // append (black, j) int x=pos[1][j]; int cost=x-F0.sum(0,x)-F1.sum(0,x); dp[i][j+1]=min(dp[i][j+1],dp[i][j]+cost); } F1.add(pos[1][j],1); } F0.add(pos[0][i],1); } cout<<dp[n][n]<<endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Sorted and Sorted |
User | fura2 |
Language | C++14 (GCC 5.4.1) |
Score | 600 |
Code Size | 1165 Byte |
Status | AC |
Exec Time | 172 ms |
Memory | 15872 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 | 135 ms | 14592 KB |
1_015.txt | AC | 21 ms | 6144 KB |
1_016.txt | AC | 104 ms | 12544 KB |
1_017.txt | AC | 41 ms | 8448 KB |
1_018.txt | AC | 2 ms | 640 KB |
1_019.txt | AC | 15 ms | 6016 KB |
1_020.txt | AC | 116 ms | 14592 KB |
1_021.txt | AC | 39 ms | 8448 KB |
1_022.txt | AC | 20 ms | 6144 KB |
1_023.txt | AC | 84 ms | 12544 KB |
1_024.txt | AC | 138 ms | 15616 KB |
1_025.txt | AC | 172 ms | 15872 KB |
1_026.txt | AC | 172 ms | 15872 KB |
1_027.txt | AC | 172 ms | 15872 KB |
1_028.txt | AC | 172 ms | 15872 KB |
1_029.txt | AC | 171 ms | 15872 KB |
1_030.txt | AC | 172 ms | 15872 KB |
1_031.txt | AC | 172 ms | 15872 KB |
1_032.txt | AC | 145 ms | 15872 KB |
1_033.txt | AC | 143 ms | 15872 KB |
1_034.txt | AC | 145 ms | 15872 KB |
1_035.txt | AC | 144 ms | 15872 KB |