Submission #971802

#TimeUsernameProblemLanguageResultExecution timeMemory
971802androArranging Shoes (IOI19_shoes)C++14
10 / 100
1 ms348 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;*/ map<int,set<int>> ms; for(int i = 0; i < n; i++) { ms[a[i]].insert(i); } vector<int> izracunao(n, 0); int ans = 0; for(int i = 0; i < n; i++) { if(izracunao[i]) { continue; } auto it = ms[-a[i]].upper_bound(i); if(it == ms[-a[i]].end()) { // znaci resio sam sve tu continue; } int idx = (*it); ms[-a[i]].erase(it); if(a[i] > 0) { //cout << 1 << " " << i << " " << idx << "\n"; izracunao[idx] = 1; ans += idx - i; } else { //cout << 2 << " " << i << " " << idx << "\n"; izracunao[idx] = 1; ans += idx - i - 1; } } return ans; } /* int main() { cout << count_swaps({- 2, 2, 2, - 2, - 2, 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...