Submission #1020237

#TimeUsernameProblemLanguageResultExecution timeMemory
1020237abdelhakimArranging Shoes (IOI19_shoes)C++14
25 / 100
1046 ms3164 KiB
#include <bits/stdc++.h> #define mod 1000000007LL #define inf 1e17 #define ll long long using namespace std; void printvec(vector<int> vec) { for (auto &&e : vec) { cout << e << ' '; } cout << endl; } ll count_swaps(vector<int> s) { ll n = s.size() / 2; bool sametype = true; for (int i = 1; i < n; i++) { if(abs(s[i])!=abs(s[i-1])) { sametype = false; break; } } if(n == 1) return s[0] > s[1]; else if(n <= 8 && !sametype) { ll ans = inf; vector<pair<ll, ll>> perm; for (int i = 0; i < 2 * n; i++) { if (s[i] > 0) { perm.push_back({-s[i], s[i]}); } } sort(perm.begin(), perm.end()); vector<int> temp = s; do { s = temp; ll subans = 0; vector<int> v; for (int i = 0; i < n; i++) { v.push_back(perm[i].first); v.push_back(perm[i].second); } map<ll,ll> index; for (int i = 0; i < 2*n; i++) { index[v[i]] = i; } for (int i = 1; i < 2*n; i++) { for (int j = 0; j < i; j++) { if(index[s[j]] > index[s[i]])subans++; } } ans = min(ans, subans); } while (next_permutation(perm.begin(), perm.end())); return ans; } else if(!sametype) return n*(n-1)/2; else { ll ans = inf; vector<pair<ll, ll>> perm; for (int i = 0; i < 2 * n; i++) { if (s[i] > 0) { perm.push_back({-s[i], s[i]}); } } sort(perm.begin(), perm.end()); vector<int> temp = s; do { s = temp; ll subans = 0; vector<int> v; for (int i = 0; i < n; i++) { v.push_back(perm[i].first); v.push_back(perm[i].second); } for (int i = 0; i < 2 * n; i++) { if (s[i] != v[i]) { for (int j = i + 1; j < 2 * n; j++) { if (v[j] == s[j - 1] && s[j] != s[j - 1]) { swap(s[j], s[j - 1]); subans++; i = -1; // This will reset the outer loop break; } swap(s[j], s[j - 1]); if (s[j] != s[j - 1]) subans++; } } } ans = min(ans, subans); } while (next_permutation(perm.begin(), perm.end())); return ans; } } // int main() // { // cout << count_swaps({-2, 2, 2, -2, -2, 2}) << endl; // }
#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...