Submission #433207

#TimeUsernameProblemLanguageResultExecution timeMemory
433207kostia244Arranging Shoes (IOI19_shoes)C++17
100 / 100
479 ms27076 KiB
#include "shoes.h" #include<bits/stdc++.h> using namespace std; using ll = long long; #define all(x) begin(x), end(x) long long count_swaps(std::vector<int> s) { ll res = 0; int n = s.size()/2; vector<ll> fen(2*n+1); auto add = [&](int x) { for(; x <= 2*n; x += x&-x) fen[x]++; }; auto get = [&](int x) { int res = 0; add(x); for(; x; x -= x&-x) res += fen[x]; return res; }; vector<int> b(2*n); map<int, vector<int>> ass; for(int i = 0; i < 2*n; i++) ass[s[i]].push_back(i); for(auto &[a, b] : ass) reverse(all(b)); vector<int> v(2*n, -69); for(int j = 0, i = 0; i < 2*n; i++) if(-69 == v[i]) { v[i] = ++j; v[ass[-s[i]].back()] = ++j; ass[s[i]].pop_back(); ass[-s[i]].pop_back(); res += s[i] > 0; } //for(auto &i : v) cout << i << " "; cout << endl; for(int i = 0; i < 2*n; i++) b[v[i]-1] = i+1; int cur = 0; for(auto i : b) { //cout << _ << " " << __ << " " << i << endl; //cout << i << " "; res += ++cur - get(i); //cout << i << " " << res << endl; } return res; }
#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...