Submission #722849

#TimeUsernameProblemLanguageResultExecution timeMemory
722849Darren0724Arranging Shoes (IOI19_shoes)C++17
100 / 100
91 ms19084 KiB
#include "shoes.h" #include<bits/stdc++.h> //#include "grader.cpp" using namespace std; int n; vector<int> v; void add(int p,int x){ for(int i=p;i<=n;i+=i&(-i)){ v[i]+=x; } } int ask(int p){ int ans=0; for(int i=p;i>0;i-=i&(-i)){ ans+=v[i]; } return ans; } int ask(int a,int b){ return ask(b+1)-ask(a); } long long count_swaps(std::vector<int> s) { n=s.size(); v.resize(n+1); vector<int> a[n+1],b[n+1]; for(int i=0;i<n;i++){ if(s[i]>0){ b[s[i]].push_back(i); } else{ a[-s[i]].push_back(i); } } vector<int> match(n); for(int i=0;i<=n;i++){ int tmp=a[i].size(); for(int j=0;j<tmp;j++){ match[a[i][j]]=b[i][j]; match[b[i][j]]=a[i][j]; } } for(int i=0;i<n;i++){ add(i+1,1); } long long ans=0; for(int i=0;i<n;i++){ if(match[i]<i){ continue; } if(s[i]<0){ int p=match[i]; ans+=ask(i+1,p-1); add(i+1,-1); add(p+1,-1); } else{ int p=match[i]; ans+=ask(i+1,p-1)+1; add(i+1,-1); add(p+1,-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...