제출 #1021263

#제출 시각아이디문제언어결과실행 시간메모리
1021263induwara16Arranging Shoes (IOI19_shoes)C++17
10 / 100
1079 ms1048576 KiB
#include "shoes.h" #include <bits/stdc++.h> using namespace std; #define itr(a) a.begin(), a.end() typedef vector<int> vi; typedef unordered_map<int, int> mii; void move(mii &chain, mii &rev, int a, int b) { chain[rev[a]] = chain[a]; rev[chain[a]] = rev[a]; chain[a] = chain[b]; rev[chain[b]] = a; chain[b] = a; rev[a] = b; } int cost(mii chain, int a, int b, int c = 0) { if (a == b) return c; c++; return cost(chain, chain[a], b, c); } long long count_swaps(vi s) { int n = s.size(), l = 0; long long c = 0; mii chain, rev; unordered_map<int, priority_queue<int>> ref; chain[-1] = 0; rev[0] = -1; for (int i = 0; i < n; i++) { if (i != n - 1) { chain[i] = i + 1; rev[i + 1] = i; } ref[s[i]].push(-i); } int fi = *find_if(itr(s), [](int i) { return i < 0; }); int f = -ref[fi].top(); l = f; if (fi != s[0]) { c += f; move(chain, rev, f, -1); } for (int i = 0; i < n / 2; i++) { int ln = s[l], li; li = -ref[-ln].top(); ref[ln].pop(); ref[-ln].pop(); if (ln < 0) { c += cost(chain, l, li) - 1; move(chain, rev, li, l); l = chain[li]; } else { c += cost(chain, l, li); move(chain, rev, li, rev[l]); l = chain[l]; } } return c; }
#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...