Submission #205043

#TimeUsernameProblemLanguageResultExecution timeMemory
205043ADJAArranging Shoes (IOI19_shoes)C++14
100 / 100
661 ms150520 KiB
#include "shoes.h" #include <iostream> #include <cstdio> #include <cstdlib> #include <vector> #include <set> #include <map> #include <algorithm> #include <bitset> #include <cmath> #include <queue> #include <stack> #include <string> #define sz(x) (int) x.size() #define all(x) x.begin(), x.end() using namespace std; const int MAXN = 2 * 105000; class Fenwick { private: vector <long long> f; public: void init(int n) { f.assign(n, 0); } void update(int pos, int delta) { for (; pos < sz(f); pos = (pos | (pos + 1))) { f[pos] += delta; } } long long sum(int pos) { long long res = 0; for (; pos >= 0; pos = (pos & (pos + 1)) - 1) { res += f[pos]; } return res; } long long sum(int l, int r) { return sum(r) - sum(l - 1); } }; int n; vector<int> s; map <int, queue<int>> mp; int a[MAXN]; Fenwick f; long long solveSameSize() { set<int> stA, stB; for (int i = 0; i < n; i++) { if (s[i] < 0) { stA.insert(i); } else { stB.insert(i); } } long long result = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { int mn = *stA.begin(); stA.erase(stA.begin()); result += max(0, mn - i); } else { int mn = *stB.begin(); stB.erase(stB.begin()); result += max(0, mn - i); } } return result; } long long solveFour() { long long result = 0; for (int i = n / 2 - 1; i >= 0; i--) { result += i; } return result; } long long solveSmall() { long long result = 0; vector<int> left, right; for (int i = 0; i < n; i += 2) { int best = -1, bestVal = 0; left.assign(n + 1, -1); right.assign(n + 1, -1); for (int j = i; j < n; j++) { int sz = abs(s[j]); if (s[j] < 0) { if (left[sz] == -1) { left[sz] = j; } } else { if (right[sz] == -1) { right[sz] = j; } } } for (int j = 1; j <= n; j++) { if (left[j] == -1 || right[j] == -1) { continue; } int cur = 0; if (left[j] < right[j]) { cur = left[j] - i + (right[j] - (i + 1)); } else { cur = left[j] - i + (right[j] - i); } if (best == -1 || cur < bestVal) { best = j; bestVal = cur; } } result += bestVal; for (int j = min(left[best], right[best]); j > i; j--) { s[j] = s[j - 1]; } for (int j = max(left[best], right[best]); j > i; j--) { s[j] = s[j - 1]; } s[i] = -best; s[i + 1] = best; /* cerr << result << endl; for (int j = 0; j < n; j++) { cerr << s[j] << " "; } cerr << endl; */ } return result; } long long count_swaps(std::vector<int> S) { s = S; n = sz(s); f.init(n); for (int i = 0; i < n; i++) { mp[s[i]].push(i); a[i] = 1; f.update(i, 1); } long long ans = 0; for (int i = 0; i < n; i++) { if (a[i] == 0) { continue; } int mnPos = mp[-s[i]].front(); // cerr << i << " " << mnPos << endl; int curNum = f.sum(0, mnPos); if (s[i] < 0) { ans += curNum - 2; } else { ans += curNum - 1; } mp[-s[i]].pop(); mp[s[i]].pop(); a[mnPos]--; f.update(mnPos, -1); a[i]--; f.update(i, -1); } return ans; // int sz = abs(s[0]); // bool sameSize = true; // for (int i = 0; i < n; i++) { // if (abs(s[i]) != sz) { // sameSize = false; // } // } // if (sameSize) { // return solveSameSize(); // } // bool ok = true; // for (int i = 0; i < n / 2; i++) { // if (s[i] > 0 || abs(s[i]) != abs(s[i + n / 2])) { // ok = false; // } // } // for (int i = n / 2; i < n; i++) { // if (s[i] < 0) { // ok = false; // } // } // if (ok) { // return solveFour(); // } // return solveSmall(); }
#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...