Submission #3012971
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define FORR(I,A,B) for(ll I = (B-1); I >= (A); --I)
const ll INF=1e18+7;
const ll MOD=1e9+7;
struct Inversions{// tentosu
// after init,u can get number of inversions num_inverstion(A)
vector<ll> L,R;
void initsize(ll n){//input size of A
L.resize(n/2+2);
R.resize(n/2+2);
}
ll merge(vector<ll> &A,ll n,ll left,ll mid,ll right){
ll i,j,k;
ll cnt = 0;
ll n1 = mid - left;
ll n2 = right - mid;
for(i=0;i<n1;i++)L[i]=A[left+i];
for(i=0;i<n2;i++)R[i]=A[mid+i];
L[n1]=R[n2]=INT_MAX;//
i=j=0;
for(k=left;k<right;k++){
if(L[i]<=R[j]){
A[k]=L[i++];
}else{
A[k]=R[j++];
cnt += n1-i;
}
}
return cnt;
}
ll mergeSort(vector<ll> &A,ll n,ll left,ll right){
ll mid;
ll v1,v2,v3;
if(left+1<right){
mid = (left+right)/2;
v1 = mergeSort(A,n,left,mid);
v2 = mergeSort(A,n,mid,right);
v3 = merge(A,n,left,mid,right);
return v1+v2+v3;
}else return 0;
}
ll num_inverstion(vector<ll> A){
vector<ll> B=A;//Aの順番を変えたくないなら
return mergeSort(B,B.size(),0,B.size());
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
ll n;
cin>>n;
vector<ll> ans(2*n);
priority_queue<P> b,r;
FOR(i,0,2*n){
ll a;char c;
cin>>c>>a;
if(c=='B'){
b.push({-a,i});
}else{
r.push({-a,i});
}
}
FOR(i,0,2*n){
if(b.size()>0&&r.size()>0){
if(b.top().second<r.top().second){
ans[b.top().second] = i;
b.pop();
}else{
ans[r.top().second] = i;
r.pop();
}
}else if(b.size()>0){
ans[b.top().second] = i;
b.pop();
}else if(r.size()>0){
ans[r.top().second] = i;
r.pop();
}
}
Inversions in;
in.initsize(2LL*n);
cout<<in.num_inverstion(ans)<<endl;
}
Submission Info
Submission Time |
|
Task |
E - Sorted and Sorted |
User |
kenta2997 |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
1900 Byte |
Status |
WA |
Exec Time |
2 ms |
Memory |
512 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 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 |
WA |
1 ms |
256 KB |
1_006.txt |
WA |
1 ms |
256 KB |
1_007.txt |
AC |
1 ms |
256 KB |
1_008.txt |
WA |
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 |
WA |
2 ms |
512 KB |
1_015.txt |
WA |
2 ms |
384 KB |
1_016.txt |
WA |
2 ms |
384 KB |
1_017.txt |
WA |
2 ms |
384 KB |
1_018.txt |
WA |
1 ms |
256 KB |
1_019.txt |
WA |
1 ms |
384 KB |
1_020.txt |
WA |
2 ms |
384 KB |
1_021.txt |
AC |
2 ms |
384 KB |
1_022.txt |
AC |
2 ms |
384 KB |
1_023.txt |
AC |
2 ms |
384 KB |
1_024.txt |
AC |
2 ms |
512 KB |
1_025.txt |
WA |
2 ms |
512 KB |
1_026.txt |
WA |
2 ms |
512 KB |
1_027.txt |
WA |
2 ms |
512 KB |
1_028.txt |
WA |
2 ms |
512 KB |
1_029.txt |
WA |
2 ms |
512 KB |
1_030.txt |
WA |
2 ms |
512 KB |
1_031.txt |
WA |
2 ms |
512 KB |
1_032.txt |
AC |
2 ms |
512 KB |
1_033.txt |
AC |
2 ms |
512 KB |
1_034.txt |
AC |
2 ms |
512 KB |
1_035.txt |
AC |
2 ms |
512 KB |