Submission #971792

#TimeUsernameProblemLanguageResultExecution timeMemory
971792androArranging Shoes (IOI19_shoes)C++14
50 / 100
1057 ms14148 KiB
#include <bits/stdc++.h>

#include "shoes.h"

using namespace std;

long long count_swaps(std::vector<int> s) {
	int n = s.size();
	map<int,int> M, M1;
	vector<int> a = s;
	for(int i = 0 ; i < n; i++) {
        if(a[i] > 0) {
            M[a[i]] += 1;
        }
        else {
            M1[a[i]] += 1;
        }
	}
	int ok = 0;
	for(int i = 0; i < n; i++) {
        if(a[i] > 0) {
            if(M[a[i]] != M1[-a[i]]) {
                ok = 1;
            }
        }
	}
	if(ok) {
        return - 1;
	}
	vector<int> izracunao(n, 0);
	int ans = 0;
	for(int i = 0; i < n; i+= 2) {
        if(a[i] > 0 && a[i] == - a[i - 1]);
        if(a[i] > 0) {
            int idx = - 1;
            for(int j = i + 1; j <= n; j++) {
                if(a[i] == - a[j]) {
                    idx = j;
                    break;
                }
            }
            //cout << 1 << " " << i << " " << idx << "\n";
            while(idx > i) {
                swap(a[idx], a[idx - 1]);
                ans += 1;
                idx -= 1;
            }
        }
        else {
            int idx = - 1;
            for(int j = i + 1; j <= n; j++) {
                if(a[i] == - a[j]) {
                    idx = j;
                    break;
                }
            }
            //cout << 2 << " " << i << " " << idx << "\n";
            while(idx > i + 1) {
                swap(a[idx], a[idx - 1]);
                ans += 1;
                idx -= 1;
            }
        }
	}
	return ans;
}

/*
int main() {
    cout << count_swaps({2, 1, - 1, - 2});
}*/
#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...