Submission #143683

#TimeUsernameProblemLanguageResultExecution timeMemory
143683qkxwsmArranging Shoes (IOI19_shoes)C++14
100 / 100
145 ms21740 KiB
#include "shoes.h"
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define MAXN 200013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N;
vi arr;
vi lt[MAXN], rt[MAXN];
vpi pos;
int A[MAXN];
ll fen[MAXN];
ll ans;

bool cmp(pii a, pii b)
{
	return a.fi + a.se < b.fi + b.se;
}
void update(int idx, ll v)
{
	for (int e = idx + 1; e <= 2 * N; e += e & (-e)) fen[e] += v;
}
ll query(int idx)
{
	ll res = 0;
	for (int e = idx + 1; e; e -= e & (-e)) res += fen[e];
	return res;
}

ll count_swaps(vi a)
{
	arr = a;
	N = SZ(arr) >> 1;
	FOR(i, 0, 2 * N)
	{
		if (arr[i] < 0) lt[-arr[i]].PB(i);
		else rt[arr[i]].PB(i);
	}
	// cerr << "HUH\n";
	FOR(i, 0, N + 1)
	{
		FOR(j, 0, SZ(lt[i]))
		{
			pos.PB({lt[i][j], rt[i][j]});
		}
	}
	//agh look at subsequences
	//ok you sort the pairs based on where they should go
	//it breaks down into counting inversions of some stupid array, after you know where everythibng should go.
	// cerr << "HI\n";
	sort(ALL(pos), cmp);
	FOR(i, 0, N)
	{
		A[2 * i] = pos[i].fi;
		A[2 * i + 1] = pos[i].se;
	}
	FOR(i, 0, 2 * N)
	{
		ans += (i - query(A[i]));
		update(A[i], 1);
	}
	return ans;
}
#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...