제출 #583684

#제출 시각아이디문제언어결과실행 시간메모리
583684joelauArranging Shoes (IOI19_shoes)C++14
100 / 100
116 ms17800 KiB
#include "shoes.h" #include <bits/stdc++.h> using namespace std; long long N, ft[200005]; vector<long long> lst1[100005], lst2[100005]; priority_queue< pair<long long,long long>, vector< pair<long long,long long> >, greater< pair<long long,long long> > > pq; void update (long long x, long long v) { x++; while (x <= N*2) ft[x] += v, x += x & -x; } long long query (long long x) { x++; long long ans = 0; while (x) ans += ft[x], x -= x & -x; return ans; } long long count_swaps(vector<int> s) { N = s.size()/2; memset(ft,0,sizeof(ft)); for (long long i = 1; i < N*2; ++i) update(i,1); for (long long i = 0; i < N*2; ++i) { if (s[i] < 0) lst1[-s[i]-1].push_back(i); else lst2[s[i]-1].push_back(i); } for (long long i = 0; i < N; ++i) if (!lst1[i].empty() && !lst2[i].empty()) { reverse(lst1[i].begin(),lst1[i].end()), reverse(lst2[i].begin(),lst2[i].end()); long long val = lst1[i].back() + lst2[i].back(); if (lst1[i].back() < lst2[i].back()) val--; pq.emplace(val,i); } long long ans = 0; for (long long i = 0; i < N; ++i) { long long x,n; tie(x,n) = pq.top(); pq.pop(); long long val = query(lst1[n].back()) + query(lst2[n].back()); if (lst1[n].back() < lst2[n].back()) val--; ans += val; update(lst1[n].back(),-1), update(lst2[n].back(),-1); lst1[n].pop_back(), lst2[n].pop_back(); if (!lst1[n].empty() && !lst2[n].empty()) { long long val = lst1[n].back() + lst2[n].back(); if (lst1[n].back() < lst2[n].back()) val--; pq.emplace(val,n); } } 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...