Submission #8395766


Source Code Expand

/*input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long 
#define double long double
#define f first
#define s second
#define mp make_pair
#define pb push_back

#define RE(i,n) for (int i = 1; i <= n; i++)
#define RED(i,n) for (int i = n; i > 0; i--)
#define REPS(i,n) for(int i = 1; (i*i) <= n; i++)
#define REP(i,n) for (int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REPD(i,n) for (int i = n-1; i >= 0; i--)
#define FORD(i,a,b) for (int i = a; i >= b; i--)

#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;
#define debug(x) cout << x << endl;
#define debug2(x,y) cout << x << " " << y << endl;
#define debug3(x,y,z) cout << x << " " << y << " " << z << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int INF = 1e18+1;
const int MOD = 1e9+7;
const double PI = 3.14159265358979323846264338;

int raise(int a,int n,int m = MOD){
  if(n == 0)return 1;
  if(n == 1)return a;
  int x = 1;
    x *= raise(a,n/2,m);
    x %= m;
    x *= x;
    x %= m;
    if(n%2)x*= a;
    x %= m;
    return x;
}

int floor1(int n,int k){
    if(n%k == 0 || n >= 0)return n/k;
    return (n/k)-1;
}

int ceil1(int n,int k){
    return floor1(n+k-1,k);
}

const int N = 2001;
int dp[N][N];
int a[2*N];
int pos[2][N];
int hehe[2*N];
int color[2*N];
int before[2*N][N];
int n;

int whatif(int color,int keepwhere,int placed){
	//cout << color << " " << keepwhere << " " << placed << endl;
	int ind = pos[color][keepwhere];
	int samecolor = hehe[ind];
	int deserve = before[ind][placed];
	//cout << ind << " " << samecolor << " " << deserve << endl;
	return ind-samecolor-deserve-1;
}

void solve(){
  	cin >> n;
  	RE(i,2*n){
  		char o;int x;cin >> o >> x;
  		//cout << (bool)(o == 'B') << " " << x << endl;
  		pos[o == 'B'][x] = i;
  		a[i] = x;
  		color[i] = (o == 'B');
  	}
  //	cout << pos[1][1] << endl;
  	int inv = 0;
  	RE(i,2*n){
  		RE(j,i-1){
  			if(color[j] == color[i]){
  				inv += a[i]<a[j];
  				hehe[i]++;
  				continue;
  			}
  			before[i][a[j]] = 1;
  		}
  		RE(j,n){
  			before[i][j] += before[i][j-1];
  		}
  	}
  	dp[0][0] = 0;
  	//dp[1][0] = whatif(0,1,0);
  	//dp[0][1] = whatif(1,1,0);
  	REP(i,n+1){
  		REP(j,n+1){
  			if(i+j == 0)continue;
  			int case1 = INF;
  			int case2 = INF;
  			if(i)case1 = dp[i-1][j]+whatif(0,i,j);
  			if(j)case2 = dp[i][j-1]+whatif(1,j,i);
  			dp[i][j] = min(case1,case2);
  		}
  	}
  	RE(i,n){
  		RE(j,n){
  	//		cout << i << " " << j << " " << dp[i][j] << endl;
  		}
  	}
  	cout << dp[n][n]+inv;
}

signed main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  //freopen(".in","r",stdin);freopen(".out","w",stdout);
  int t = 1;
  //cin >> t;
  while(t--){
    solve();
  }
  return 0;
}

Submission Info

Submission Time
Task E - Sorted and Sorted
User shashwatchandra
Language C++14 (GCC 5.4.1)
Score 600
Code Size 3281 Byte
Status AC
Exec Time 106 ms
Memory 94208 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 2304 KB
0_001.txt AC 1 ms 2304 KB
0_002.txt AC 2 ms 2432 KB
1_003.txt AC 2 ms 2304 KB
1_004.txt AC 2 ms 2304 KB
1_005.txt AC 1 ms 2304 KB
1_006.txt AC 2 ms 2432 KB
1_007.txt AC 2 ms 2432 KB
1_008.txt AC 2 ms 2432 KB
1_009.txt AC 2 ms 2304 KB
1_010.txt AC 2 ms 2304 KB
1_011.txt AC 2 ms 2304 KB
1_012.txt AC 2 ms 2304 KB
1_013.txt AC 2 ms 2304 KB
1_014.txt AC 83 ms 88320 KB
1_015.txt AC 15 ms 34304 KB
1_016.txt AC 63 ms 76032 KB
1_017.txt AC 28 ms 48896 KB
1_018.txt AC 3 ms 7040 KB
1_019.txt AC 11 ms 28032 KB
1_020.txt AC 73 ms 82176 KB
1_021.txt AC 33 ms 50944 KB
1_022.txt AC 17 ms 38400 KB
1_023.txt AC 59 ms 76032 KB
1_024.txt AC 79 ms 93568 KB
1_025.txt AC 106 ms 94208 KB
1_026.txt AC 105 ms 94208 KB
1_027.txt AC 105 ms 94208 KB
1_028.txt AC 106 ms 94208 KB
1_029.txt AC 105 ms 94208 KB
1_030.txt AC 105 ms 94208 KB
1_031.txt AC 105 ms 94208 KB
1_032.txt AC 104 ms 94208 KB
1_033.txt AC 101 ms 94208 KB
1_034.txt AC 95 ms 94208 KB
1_035.txt AC 81 ms 94208 KB