Submission #153500

#TimeUsernameProblemLanguageResultExecution timeMemory
153500myungwooArranging Shoes (IOI19_shoes)C++14
100 / 100
451 ms275096 KiB
#include <bits/stdc++.h>
#include "shoes.h"
using namespace std;

#define MAXN 200005
typedef long long lld;

int N;
int A[MAXN], bit[MAXN];
bool vis[MAXN];
stack <int> L[MAXN], R[MAXN], order;

void add(int n){
	vis[n] = 1;
	for (;n<=N;n+=n&-n) bit[n]++;
}
int get(int n)
{
	int ret = 0;
	for (;n;n^=n&-n) ret += bit[n];
	return ret;
}

int move(int a, int b)
{
	// a -> b
	add(a);
	return b-(a-get(a-1));
}

lld count_swaps(vector<int> arr)
{
	N = arr.size();
	for (int i=1;i<=N;i++) A[i] = arr[i-1], bit[i] = 0;
	for (int i=1;i<=N;i++){
		if (A[i] < 0) L[-A[i]].push(i);
		if (A[i] > 0) R[A[i]].push(i);
	}
	lld total = 0;
	int pos = N;
	for (int i=N;i;i--){
		while (vis[i]) i--;
		if (!i) break;
		int t = abs(A[i]);
		total += move(R[t].top(), pos);
		total += move(L[t].top(), pos-1);
		R[t].pop(); L[t].pop();
		pos -= 2;
	}
	return total;
}
#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...