제출 #604588

#제출 시각아이디문제언어결과실행 시간메모리
604588pawnedArranging Shoes (IOI19_shoes)C++17
100 / 100
341 ms28716 KiB
#include "shoes.h"

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;

typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

long long count_swaps(std::vector<int> s) {
	ll N = s.size() / 2;
	int shoes[2 * N];
	for (int i = 0; i < 2 * N; i++) {
		shoes[i] = s[i];
	}
	int order[N];	// order of processing shoes
	int pm[N + 1] = {0};
	int curr = 0;
	for (int i = 0; i < 2 * N; i++) {
		int val = abs(shoes[i]);
		bool toadd = true;
		if (pm[val] < 0 && shoes[i] > 0)
			toadd = false;
		if (pm[val] > 0 && shoes[i] < 0)
			toadd = false;
		if (toadd) {
			order[curr] = val;
			curr++;
			if (shoes[i] > 0)
				pm[val]++;
			else
				pm[val]--;
		}
		else {
			if (shoes[i] > 0)
				pm[val]++;
			else
				pm[val]--;
		}
	}
/*
	cout<<"order: ";
	for (int i = 0; i < N; i++) {
		cout<<order[i]<<" ";
	}
	cout<<endl;
*/
	map<ii, int> final;
	int counter[N + 1] = {0};
	for (int i = 0; i < N; i++) {
		final[{-order[i], counter[order[i]]}] = 2 * i;
		final[{order[i], counter[order[i]]}] = 2 * i + 1;
		counter[order[i]]++;
	}
	int notsort[2 * N];
	int counter2[2 * N + 1] = {0};	// subtract N after
	for (int i = 0; i < 2 * N; i++) {
		ii val = {shoes[i], counter2[shoes[i] + N]};
		counter2[shoes[i] + N]++;
		notsort[i] = final[val];
	}
/*
	cout<<"notsort: ";
	for (int i = 0; i < 2 * N; i++) {
		cout<<notsort[i]<<" ";
	}
	cout<<endl;
*/
	pbds T;
	ll total = 0;
	for (int i = 0; i < 2 * N; i++) {
		T.insert(notsort[i]);
		total += T.order_of_key(notsort[i]);
	}
	return total;
}
#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...