제출 #826901

#제출 시각아이디문제언어결과실행 시간메모리
826901vjudge1Arranging Shoes (IOI19_shoes)C++17
50 / 100
1096 ms258316 KiB
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define en '\n'
#define all(v) v.begin(),v.end()
#define pii pair <int,int>
#define f first
#define s second
#define mp make_pair
const int N = 3e5 + 10;
int a[N],n;
deque <int> pos[N];
int t[N * 4];
void upd(int v,int tl,int tr,int pos) {
	if(tl == tr) {
		t[v] = 1;
		return;
	}
	int tm = (tl + tr) / 2;
	if(pos <= tm)upd(v + v,tl,tm,pos);
	else upd(v + v + 1,tm + 1,tr,pos);
	t[v] = t[v + v] + t[v + v + 1];
}
int get(int v,int tl,int tr,int l,int r) {
	if(tl > r || tr < l)return 0;
	if(tl >= l && tr <= r)return t[v];
	int tm = (tl + tr) / 2;
	return get(v + v,tl,tm,l,r) + get(v + v + 1,tm + 1,tr,l,r);
}
bool cmp(pii x,pii y) {
	return x.f + x.s < y.f + y.s;
}
vector <pii> g;
void rebuild(int L) {
	g.clear();
	vector <int> q;
	for(int i = L;i <= 2 * n;i++) {
		if(a[i] > 0)pos[a[i]].pb(i);
		else q.pb(i);
	}
	for(auto to : q) {
		int val = -a[to];
		int cor = pos[val].front();
		pos[val].pop_front();
		g.pb(mp(to,cor));
		//cout << to << " " << cor << en;
	}
}
ll count_swaps(vector<int> S) {
	n = S.size();
	vector <int> q;
	for(int i = 0;i < n;i++) {
		a[i + 1] = S[i];
	}
	n /= 2;
	rebuild(1);
	ll ans1 = 0;
	for(int i = 1;i <= n;i++) {
		pair <int,pii> ans = mp(5 * n,mp(0,0));
		int L = i * 2 - 1,R = i * 2;
		for(auto to : g) {
			int val = to.f - L + to.s - R + (to.f > to.s);
			ans = min(ans,mp(val,to));
		}
		ans1 += ans.f;
		for(int i = ans.s.f - 1;i >= L;i--) {
			swap(a[i],a[i + 1]);
		}
		if(ans.s.f > ans.s.s)ans.s.s++;
		for(int i = ans.s.s - 1;i >= R;i--) {
			swap(a[i],a[i + 1]);
		}
		//cout << ans.f << " " << ans.s.f << " " << ans.s.s << en;
		rebuild(2 * i + 1);
	}
	return ans1;
}
#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...