Submission #594199

#TimeUsernameProblemLanguageResultExecution timeMemory
594199senthetaArranging Shoes (IOI19_shoes)C++17
100 / 100
246 ms143560 KiB
#include "shoes.h"
// author : sentheta aka vanwij
#include<iostream>
#include<algorithm>
#include<cassert>
#include<random>
#include<chrono>
#include<cmath>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;

#define Int long long
#define V vector
#define pii pair<int,int>
#define ff first
#define ss second

#define rand() (uniform_int_distribution<int>(0,1<<30)(rng))
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define pow2(x) (1LL<<(x))
#define msb(x) (63-__builtin_clzll(x))
#define bitcnt(x) (__builtin_popcountll(x))

#define nl '\n'
#define _ << ' ' <<
#define all(x) (x).begin(), (x).end()
#define rep(i,a,b) for(Int i = (Int)(a); i < (Int)(b); i++)
#define dbg(x) if(1) cout << "?" << #x << " : " << (x) << endl << flush;

const int N = 1e5+5;

Int n;
V<int> s;

queue<Int> pos[N], neg[N];

#define mid ((tl+tr)/2)
#define lc (v+1)
#define rc (v+2*(mid-tl+1))
struct Segtree{
	Int st[4*N];
	Int qry(Int l,Int r,Int v=0,Int tl=0,Int tr=2*n-1){
		if(tr<l || r<tl) return 0;
		if(l<=tl && tr<=r) return st[v];
		return qry(l,r, lc,tl,mid)
			+ qry(l,r, rc,mid+1,tr);
	}
	void upd(Int i,Int x,Int v=0,Int tl=0,Int tr=2*n-1){
		if(tl==tr){
			st[v] = x; return;
		}
		if(i<=mid) upd(i,x, lc,tl,mid);
		else upd(i,x, rc,mid+1,tr);
		st[v] = st[lc] + st[rc];
	}
} segtree;

bool taken[2*N];

Int count_swaps(V<int> _s){
	s = _s; n = s.size()/2;

	rep(i,0,2*n){
		segtree.upd(i,1);

		if(s[i]>0){
			pos[s[i]].push(i);
		}
		else{
			neg[-s[i]].push(i);
		}
	}

	Int ans = 0;
	rep(i,0,2*n) if(!taken[i]){
		Int x = abs(s[i]), j;

		if(s[i] > 0){
			j = neg[x].front();
			ans ++;
		}
		else{
			j = pos[x].front();
		}

		if(i+1 <= j-1){
			ans += segtree.qry(i+1, j-1);
		}
		segtree.upd(i,0);
		segtree.upd(j,0);

		taken[i] = taken[j] = 1;
		pos[x].pop();
		neg[x].pop();
	}

	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...