Submission #1134568

#TimeUsernameProblemLanguageResultExecution timeMemory
1134568ackhavaSorting (IOI15_sorting)C++20
100 / 100
834 ms27632 KiB
#include "sorting.h" #include <bits/stdc++.h> #define REP(v, i, j) for (int v = i; v != j; v++) #define FORI(v) for (auto i : v) #define FORJ(v) for (auto j : v) #define OUT(v, a) \ FORI(v) \ cout << i << a; #define OUTS(v, a, b) \ cout << v.size() << a; \ OUT(v, b) #define in(a, n) \ REP(i, 0, n) \ cin >> a[i]; #define SORT(v) sort(begin(v), end(v)) #define REV(v) reverse(begin(v), end(v)) #define MEMSET(m) memset(m, -1, sizeof m) #define pb push_back #define fi first #define se second #define detachIO \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); using namespace std; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef pair<pii, pii> piiii; const int MOD = 1e9+7; struct modint { long long val; modint() = default; modint(int _val): val(_val){} modint operator+(modint b){ return ((this->val + b.val)%MOD); } modint operator-(modint b){ return ((MOD + this->val - b.val)%MOD); } modint operator*(modint b){ return ((this->val * b.val)%MOD); } modint operator^(int a){ if(a==0)return 1; if(a==1)return *this; return (((*this)*(*this))^(a>>1))*((*this)^(a&1)); } }; modint invert(modint a){ return a^(MOD-2); } modint operator/(modint a, modint b){ return a*invert(b); } int amp[200100]; int vmp[200100]; int va[200100]; int _findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { unordered_set<int> off; vector<int> vec,v2; REP(i,0,N){ vec.pb(i); } v2=vec; REP(i,0,M){ swap(vec[X[M-i-1]],vec[Y[M-i-1]]); } REP(i,0,M){ swap(vec[X[i]],vec[Y[i]]); } assert(v2==vec); REP(i,0,M){ swap(vec[X[M-i-1]],vec[Y[M-i-1]]); } vector<int> arr,a2; REP(i,0,N)arr.pb(S[i]); a2=arr; REP(i,0,M){ P[i]=Q[i]=0; } REP(i,0,N){ amp[arr[i]]=i; vmp[vec[i]]=i; } REP(i,0,N){ if(arr[i]!=vec[i])off.insert(arr[i]); } REP(i,0,M){ if(!off.size())break; int v=Y[i]; int a=X[i]; amp[arr[a]]=-1; amp[arr[v]]=-1; vmp[vec[a]]=-1; vmp[vec[v]]=-1; swap(arr[a],arr[v]); swap(vec[a],vec[v]); amp[arr[a]]=a; amp[arr[v]]=v; vmp[vec[a]]=a; vmp[vec[v]]=v; if(!off.size())break; auto x=*off.begin(); a=amp[x]; v=vmp[x]; amp[arr[a]]=-1; amp[arr[v]]=-1; vmp[vec[a]]=-1; vmp[vec[v]]=-1; swap(arr[a],arr[v]); amp[arr[a]]=a; amp[arr[v]]=v; vmp[vec[a]]=a; vmp[vec[v]]=v; if(arr[a]==vec[a])off.erase(arr[a]); if(arr[v]==vec[v])off.erase(arr[v]); P[i]=a; Q[i]=v; } // if(arr!=vec)return -1; // if(off.size())return -1; SORT(arr); // if(a2==arr)return 0; // off.clear(); // REP(i,0,N){ // if(a2[i]!=arr[i])off.insert(a2[i]),off.insert(arr[i]); // } REP(i,0,M){ swap(a2[X[i]],a2[Y[i]]); swap(a2[P[i]],a2[Q[i]]); // if(a2[X[i]]==arr[X[i]])off.erase(a2[X[i]]); // if(a2[Y[i]]==arr[Y[i]])off.erase(a2[Y[i]]); // if(a2[P[i]]==arr[P[i]])off.erase(a2[P[i]]); // if(a2[Q[i]]==arr[Q[i]])off.erase(a2[Q[i]]); // if(a2[X[i]]!=arr[X[i]])off.insert(a2[X[i]]),off.insert(arr[X[i]]); // if(a2[Y[i]]!=arr[Y[i]])off.insert(a2[Y[i]]),off.insert(arr[Y[i]]); // if(a2[P[i]]!=arr[P[i]])off.insert(a2[P[i]]),off.insert(arr[P[i]]); // if(a2[Q[i]]!=arr[Q[i]])off.insert(a2[Q[i]]),off.insert(arr[Q[i]]); // if(!off.size())return i+1; } if(a2==arr)return M; return -1; } int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { int l=0,r=M; int ans=M; assert(_findSwapPairs(N,S,M,X,Y,P,Q)!=-1); while(l<=r){ int m=(l+r)/2; if(_findSwapPairs(N,S,m,X,Y,P,Q)==-1)l=m+1; else r=m-1,ans=m; } return _findSwapPairs(N,S,ans,X,Y,P,Q); }
#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...