제출 #242412

#제출 시각아이디문제언어결과실행 시간메모리
242412quocnguyen1012Arranging Shoes (IOI19_shoes)C++14
10 / 100
8 ms5120 KiB
#ifndef LOCAL #include "shoes.h" #endif // LOCAL #include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define ar array using namespace std; typedef long long ll; typedef pair<int, int> ii; const int maxn = 1e5 + 5; int N; vector<int> same[2 * maxn]; int bit[2 * maxn], go[2 * maxn]; int rem[2 * maxn]; void upd(int i, int v) { for(++i; i < 2 * maxn; i += i & -i) bit[i] += v; } int sum(int i) { int res = 0; for(++i; i; i -= i & -i) res += bit[i]; return res; } long long count_swaps(vector<int> a) { ll res = 0; N = a.size(); for(int i = 0; i < N; ++i){ if(same[abs(a[i])].empty() || a[same[abs(a[i])].back()] == a[i]){ same[abs(a[i])].eb(i); if(a[i] > 0) ++res; } else{ go[i] = same[abs(a[i])].back(); go[same[abs(a[i])].back()] = i; same[abs(a[i])].pop_back(); } } //cerr << res << '\n'; fill(rem, rem + N, 1); for(int i = 0; i < N; ++i) upd(i, 1); for(int i = 0; i < N; ++i){ assert(same[i].empty()); } for(int i = 0; i < N; ++i){ if(rem[i]){ int nxt = go[i]; res += sum(nxt - 1) - 1; rem[i] = rem[nxt] = 0; upd(i, -1); upd(nxt, -1); } } return res; } #ifdef LOCAL signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL freopen("A.INP", "r", stdin); freopen("A.OUT", "w", stdout); #endif // LOCAL int n; cin >> n; vector<int> a(2 * n); for(auto & it : a) cin >> it; cout << count_swaps(a); } #endif // LOCAL
#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...