제출 #501663

#제출 시각아이디문제언어결과실행 시간메모리
501663BY_KUTBILIMArranging Shoes (IOI19_shoes)C++17
100 / 100
244 ms274436 KiB
#include <bits/stdc++.h>
using namespace std;
#define bit(s, i) (s & (1 << i))
#define fi first
#define se second
#define pb push_back
#define endl '\n'

const int N = 2e5 + 5;
int Bit[N], cur[N];
queue<int> pos[N][2];
void update(int i){
	for(; i < N; i += (i & -i))Bit[i]++;
}
int get(int i){
	int res = 0;
	for(; i > 0; i -= (i & -i)) res += Bit[i];
	return res;
}
long long count_swaps(vector<int> a){
	int n = a.size() / 2;
	vector<int> solve;
	for(int i = 0; i < 2 * n; i++){
		int x = a[i];
		if(x < 0){
			if(cur[abs(x)] <= 0)solve.pb(abs(x));
			pos[abs(x)][0].push(i+1);
			cur[abs(x)]--;
		} else{
			if(cur[abs(x)] >= 0)solve.pb(abs(x));
			pos[abs(x)][1].push(i+1);
			cur[abs(x)]++;
		}
	}
	long long res = 0;
	int cnt = 0;
	for(int x : solve){
		int le = pos[x][0].front(), ri = pos[x][1].front();
		pos[x][0].pop(), pos[x][1].pop();
		if(le > ri){
			res++;
			swap(le,ri);
		}
		res += le + ri;
		res -= 2 * (cnt + 2) - 1;

		res -= get(le) - cnt;
		update(le), cnt++;
		res -= get(ri) - cnt;
		update(ri), cnt++;
	}
	return res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...