Submission #651932

#TimeUsernameProblemLanguageResultExecution timeMemory
651932jiahngArranging Shoes (IOI19_shoes)C++14
100 / 100
135 ms141304 KiB
#include "shoes.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pi; typedef vector <int> vi; typedef vector <pi> vpi; typedef pair<pi, ll> pii; typedef set <ll> si; typedef long double ld; #define f first #define s second #define mp make_pair #define FOR(i,s,e) for(int i=s;i<=int(e);++i) #define DEC(i,s,e) for(int i=s;i>=int(e);--i) #define pb push_back #define all(x) (x).begin(), (x).end() #define lbd(x, y) lower_bound(all(x), y) #define ubd(x, y) upper_bound(all(x), y) #define aFOR(i,x) for (auto i: x) #define mem(x,i) memset(x,i,sizeof x) #define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define maxn 200010 #define INF (ll)1e9 #define MOD 1000000007 typedef pair <vi, int> pvi; typedef pair <int,pi> ipi; typedef vector <pii> vpii; int N; queue <int> A[maxn]; int fw[maxn]; int qry(int p){ int ans = 0; for (int i = p; i > 0; i -= i & (-i)) ans += fw[i]; return ans; } void upd(int p, int v){ for (int i = p; i <= N; i += i & (-i)) fw[i] += v; } long long count_swaps(std::vector<int> s) { vpi v; N = s.size(); FOR(i,1,s.size()){ int val = (s[i-1] < 0 ? -i : i); //cout << val << ' '; if (A[abs(s[i-1])].empty() || (A[abs(s[i-1])].front() < 0) == (val < 0)){ A[abs(s[i-1])].push(val); }else{ if (val < 0){ v.pb(pi(-val, A[abs(s[i-1])].front())); }else{ v.pb(pi(-A[abs(s[i-1])].front(),val)); } A[abs(s[i-1])].pop(); } } ll ans = 0; FOR(i,0,v.size()-1){ if (v[i].f > v[i].s){ ans++; swap(v[i].f,v[i].s); } } sort(all(v)); /* FOR(i,0,v.size()-1) FOR(j,i+1,v.size()-1){ if (v[j].s < v[i].s) ans += 2; else if (v[j].f < v[i].s) ans++; } return ans; */ /* sort(all(v), [](pi i,pi j){ return (i.f < j.f) + (i.f < j.s) + (i.s < j.f) + (i.s < j.s) >= 2; }); */ vi v2; aFOR(i, v){ v2.pb(i.f); v2.pb(i.s); } aFOR(i, v2){ ans += qry(N) - qry(i); upd(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...