제출 #766719

#제출 시각아이디문제언어결과실행 시간메모리
766719khshgArranging Shoes (IOI19_shoes)C++14
65 / 100
107 ms7364 KiB
#include<bits/stdc++.h>
using namespace std;
 
using ll = long long;
using ld = long double;
using str = string;
 
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
#define mp make_pair
#define ff first
#define ss second
 
#define ar array
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<ld>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;
 
#define sz(x) (int)((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
 
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for(int i = (b) - 1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define rep(a) F0R(_, a)
#define trav(a, x) for(auto& a : x)
 
template<class T> bool ckmin(T& a, const T& b) { return (b < a ? a = b, 1 : 0); }
template<class T> bool ckmax(T& a, const T& b) { return (b > a ? a = b, 1 : 0); }

const int maxn = 2e5 + 3;
int st[2][maxn];

void add(bool f, int ind, int delta) {
	++ind;
	while(ind < maxn) {
		st[f][ind] += delta;
		ind += ind & -ind;
	}
}

int val(bool f, int ind) {
	++ind;
	int ans = 0;
	while(ind > 0) {
		ans += st[f][ind];
		ind -= ind & -ind;
	}
	return ans;
}

ll count_swaps(vi S) {
	int N = sz(S) >> 1;
	{
		map<int, int> c;
		trav(u, S) c[abs(u)];
		int tim3 = 1; trav(u, c) u.ss = tim3++;
		trav(u, S) u = c[abs(u)] * (u < 0 ? -1 : 1);
	}
	if(N <= 8) {

	vi A; trav(u, S) if(u > 0) A.eb(u);
	sor(A);
	ll ans = 0x3f3f3f3f;
	do {
		V<vi> l(N + 1), r(N + 1);
		F0R(i, 2 * N) {
			if(S[i] < 0) l[-S[i]].eb(i);
			else r[S[i]].eb(i);
		}
		ll cur = 0;
		F0R(i, N) {
			int lsh = 2 * i;
			int dis = 0x3f3f3f3f, ind = -1;
			F0R(j, sz(l[A[i]])) {
				if(ckmin(dis, abs(lsh - l[A[i]][j]))) ind = j;
			}
			if(ind == -1) {
				cout << "wtf\n";
				exit(1);
			}
			cur += dis;
			FOR(j, 1, N + 1) {
				trav(u, l[j]) if(u < l[A[i]][ind]) ++u;
				trav(u, r[j]) if(u < l[A[i]][ind]) ++u;
			}
			l[A[i]].erase(bg(l[A[i]]) + ind);
			lsh = 2 * i + 1;
			dis = 0x3f3f3f3f, ind = -1;
			F0R(j, sz(r[A[i]])) {
				if(ckmin(dis, abs(lsh - r[A[i]][j]))) ind = j;
			}
			if(ind == -1) {
				cout << "wtf\n";
				exit(1);
			}
			cur += dis;
			FOR(j, 1, N + 1) {
				trav(u, l[j]) if(u < r[A[i]][ind]) ++u;
				trav(u, r[j]) if(u < r[A[i]][ind]) ++u;
			}
			r[A[i]].erase(bg(r[A[i]]) + ind);
		}
		ckmin(ans, cur);
	} while(next_permutation(all(A)));
	return ans;
	}
	vi rs;
	vi ls;
	F0R(i, 2 * N) {
		if(S[i] > 0) rs.pb(i);
		else ls.pb(i);
	}
	add(0, 0, ls[0]);
	add(1, 0, rs[0]);
	FOR(i, 1, sz(ls)) {
		add(0, i, ls[i] - ls[i - 1]);
	}
	FOR(i, 1, sz(rs)) {
		add(1, i, rs[i] - rs[i - 1]);
	}
	ll ans = 0;
	R0F(i, 2 * N) {
		if(i & 1) {
			int ind = val(1, sz(rs) - 1);
			rs.pop_back();
			if(i == ind) continue;
			ans += i - ind;
			int tl = 0, tr = sz(ls) - 1;
			while(tl < tr) {
				int tm = (tl + tr) / 2;
				if(val(0, tm) < ind) {
					tl = tm + 1;
				} else {
					tr = tm;
				}
			}
			add(0, tl, -1);
		} else {
			int ind = val(0, sz(ls) - 1);
			ls.pop_back();
			if(i == ind) continue;
			ans += i - ind;
			int tl = 0, tr = sz(rs) - 1;
			while(tl < tr) {
				int tm = (tl + tr) / 2;
				if(val(1, tm) < ind) {
					tl = tm + 1;
				} else {
					tr = tm;
				}
			}
			add(1, tl, -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...