제출 #750940

#제출 시각아이디문제언어결과실행 시간메모리
750940anaduguleanuArranging Shoes (IOI19_shoes)C++14
100 / 100
170 ms139792 KiB
#include <iostream> #include <queue> #define MAX 200000 using namespace std; ///ifstream cin ("c.in"); ///ofstream cout ("c.out"); queue <int> pos[MAX + 10]; int tree[MAX + 10], visited[MAX + 10]; void update(int pos, int val) { for (int i = pos; i <= MAX; i = i + (i & -i)) tree[i] = tree[i] + val; } int query(int pos) { int ans = 0; for (int i = pos; i >= 1; i = i - (i & -i)) ans = ans + tree[i]; return ans; } long long count_swaps(vector <int> v) { for (int i = 1; i <= MAX; i++) tree[i] = (i & -i); int n = v.size(); v.insert(v.begin(), 0); for (int i = 1; i <= n; i++) { if (v[i] < 0) v[i] = v[i] + n + 1; pos[v[i]].push(i); } long long ans = 0; for (int i = 1; i <= n; i++) if (visited[i] == 0) { int current = v[i], next = n - v[i] + 1; if (v[i] > n / 2) ans--; int nextpos = pos[next].front(); pos[next].pop(); pos[current].pop(); ans = ans + query(nextpos) - query(i); update(nextpos, -1); visited[nextpos] = 1; visited[i] = 1; } return ans; }
#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...