제출 #151925

#제출 시각아이디문제언어결과실행 시간메모리
151925ivandasfsArranging Shoes (IOI19_shoes)C++14
100 / 100
221 ms23720 KiB
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>

using namespace std;

typedef long long ll;

const int OFF = 1e5;

int off = 1;

set<int> s[200005];
int tur[600005];
//int a[200005];

void update(int node, int val) {
	if (!node) return ;
	if (node>=off) {
		tur[node] = val;
	}else {
		tur[node] = tur[node*2] + tur[node*2+1];
	}
	update(node/2, val);
}

int query(int l, int r, int x, int y, int node) {
	if (x>r or y<l) return 0;
	if (l>=x and r<=y) return tur[node];
	return query(l, (l+r)/2, x, y, node*2)+query((l+r)/2+1, r, x, y, node*2+1);
}

ll count_swaps(vector<int> a) {
	int n = a.size();
	while (off<n) off*=2;
	for (int i=0 ; i<n ; i++) {
		update(off+i, 1);
		s[a[i]+OFF].insert(i);
	}
	ll sol=0;
	for (int i=0 ; i<n ; i++) {
		if (!a[i]) continue;
		//cout <<a[i]<<endl;
		int pos=*s[-a[i]+OFF].begin();
		//cout <<i<<":"<<pos<<endl;
		int g=query(0, off-1, i, pos, 1)-2;
		if (a[i]>0) g++;
		sol+=g;
		//cout <<"sol="<<sol<<endl;
		update(pos+off, 0);
		s[a[i]+OFF].erase(s[a[i]+OFF].begin());
		s[-a[i]+OFF].erase(s[-a[i]+OFF].begin());
		a[pos]=0;
	}
	return sol;
}
#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...