Submission #398581

#TimeUsernameProblemLanguageResultExecution timeMemory
398581richyrichArranging Shoes (IOI19_shoes)C++17
50 / 100
159 ms17536 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD = 1e9+7;
const ll N = 1e5+10;
const ll inf = 1e9+100;
const double eps = 1;
int n, a[N];
set<int> L[N], R[N];
ll ans;
long long count_swaps(std::vector<int> s) {
	/*for(int c : s) printf("%d ", c);
	printf("\n");*/
	ans = 0;
    n = s.size();
    for(int i = 1;i <= n;i++) {
    	a[i] = s[i-1];
    	if(a[i] < 0) L[-a[i]].insert(i);
    	else R[a[i]].insert(i);
    }
    /*for(int i = 1; i <= 2; i++) {
    	printf("left %d:", i);
    	for(int c : L[i]) printf("%d ", c);
    	printf("\n");
    }
    for(int i = 1; i <= 2; i++) {
    	printf("right %d:", i);
    	for(int c : R[i]) printf("%d ", c);
    	printf("\n");
    }*/
    for(int i = 1; i <= n; i += 2){
    	int pos, req;
    	if(a[i] < 0) {
    		pos = *R[-a[i]].begin();
    		L[-a[i]].erase(i);
    		R[-a[i]].erase(pos);
    		req = i+1;
    	}
    	else {
    		pos = *L[a[i]].begin();
    		R[a[i]].erase(i);
    		L[a[i]].erase(pos);
    		req = i;
    	}
    	for(int j = pos - 1; j > i; j--) {
    		if(a[j] < 0) {
    			L[-a[j]].erase(j);
    			L[-a[j]].insert(j+1);
    		}
    		else {
    			R[a[j]].erase(j);
    			R[a[j]].insert(j+1);
    		}
    	}
    	for(int j = pos; j > req; j--) {
    		ans++;
    		swap(a[j], a[j-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...