Submission #493610

#TimeUsernameProblemLanguageResultExecution timeMemory
493610protonArranging Shoes (IOI19_shoes)C++17
100 / 100
531 ms148560 KiB
#include <bits/stdc++.h> using namespace std; long long merge(int a[], int beg, int mid, int end) { int i = beg, j = mid; long long inv = 0; while(i < mid and j < end) if(a[i] < a[j]) i++; else inv += mid - i, j++; std::inplace_merge(a + beg, a + mid, a + end); return inv; } // sort arr[beg:end) and return no. of inversions long long inversions(int a[], int beg, int end) { long long inv = 0; if(end - beg < 2) return 0; if(end - beg == 2) { if(a[beg] < a[beg + 1]) return 0; else return swap(a[beg], a[beg + 1]), 1; } int mid = (beg + end) >> 1; inv += inversions(a, beg, mid); inv += inversions(a, mid, end); inv += merge(a, beg, mid, end); return inv; } long long count_swaps(vector<int> a) { int n = (int) a.size(); int p[n]; map<int, queue<int>> mp; for(int i = 0, c = 0; i < n; i++) { if(mp[a[i]].empty()) { if(a[i] > 0) { mp[-a[i]].push(c); p[i] = c + 1; } else { p[i] = c; mp[-a[i]].push(c + 1); } c += 2; } else { p[i] = mp[a[i]].front(); mp[a[i]].pop(); } } return inversions(p, 0, n); } // int main() // { // int n; cin >> n; // vector<int> v(n + n); // for(int &x: v) cin >> x; // cout << count_swaps(v); // }
#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...