Submission #219178

#TimeUsernameProblemLanguageResultExecution timeMemory
219178sidiq_haArranging Shoes (IOI19_shoes)C++14
100 / 100
190 ms139328 KiB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "shoes.h"
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(v) v.begin(), v.end()

using namespace std;
typedef long long LL;
const LL MOD = 1e9 + 7;
const double PI = 2 * acos(0);
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set;
//order_of_key(x) = number of elements stricly smaller than x
//find_by_order(x) = return x-th largest element
int BIT[200001];

void update(int p) {
	for (int i = p; i <= 2e5; i += (i & (-i))) BIT[i]++;
}

int query(int p) {
	int cnt = 0;
	for (int i = p; i > 0; i -= (i & (-i))) {
		cnt += BIT[i];
	}
	return cnt;
}

bool flag[200001];
queue<int> kiri[100001], kanan[100001];

long long count_swaps(vector<int> s) {
	int n = s.size();
	LL ans2 = 0;
	vector<int> ans;
	for (int i = 0; i < n; i++) {
		if (s[i] < 0) kiri[-s[i]].push(i);
		else kanan[s[i]].push(i);
	}
	for (int i = 0; i < n; i++) {
		if (!flag[i]) {
			if (s[i] < 0) {
				int x = -s[i];
				flag[i] = 1;
				ans.pb(i);
				kiri[x].pop();
				int xx = kanan[x].front();
				ans.pb(xx);
				flag[xx] = 1;
				kanan[x].pop();
			} else {
				int x = s[i];
				flag[i] = 1;
				kanan[x].pop();
				int xx = kiri[x].front();
				ans.pb(xx);
				ans.pb(i);
				flag[xx] = 1;
				kiri[x].pop();
			}
		}
	}
	for (int i = 0; i < n; i++) {
		int cnt = query(ans[i] + 1);
		cnt = i - cnt;
		ans2 += (ans[i] + cnt - i);
		update(ans[i] + 1);
	}
	return ans2;
}
#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...