Submission #1248211

#TimeUsernameProblemLanguageResultExecution timeMemory
1248211kl0989e정렬하기 (IOI15_sorting)C++20
100 / 100
210 ms43672 KiB
#include "sorting.h" #include <bits/stdc++.h> using namespace std; #define ll long long #define pi pair<int, int> #define pl pair<ll, ll> #define vi vector<int> #define vl vector<ll> #define fi first #define se second #define pb push_back #define all(x) (x).begin(),(x).end() bool check(int n, int s[], int r, int x[], int y[], int p[], int q[]) { for (int i=0; i<r; i++) { swap(s[x[i]],s[y[i]]); swap(s[p[i]],s[q[i]]); } bool ok=1; for (int i=0; i<n; i++) { ok&=(s[i]==i); } return ok; } int solve(int n, int s[], int m, int x[], int y[], int p[], int q[]) { if (n==1) { return 0; } int ss[n]; for (int i=0; i<n; i++) { ss[i]=s[i]; } vi goesto(n); iota(all(goesto),0); for (int i=m-1; i>=0; i--) { swap(goesto[x[i]],goesto[y[i]]); } vi to(n); vi getpos(n); for (int i=0; i<n; i++) { to[goesto[i]]=i; getpos[s[i]]=i; } int ind=0; for (int i=0; i<n && ind<m; i++) { swap(s[x[ind]],s[y[ind]]); swap(getpos[s[x[ind]]],getpos[s[y[ind]]]); swap(goesto[x[ind]],goesto[y[ind]]); swap(to[goesto[x[ind]]],to[goesto[y[ind]]]); if (s[to[i]]==i) { swap(s[x[ind]],s[y[ind]]); swap(getpos[s[x[ind]]],getpos[s[y[ind]]]); swap(goesto[x[ind]],goesto[y[ind]]); swap(to[goesto[x[ind]]],to[goesto[y[ind]]]); continue; } p[ind]=to[i]; q[ind]=getpos[i]; swap(s[p[ind]],s[q[ind]]); swap(getpos[s[p[ind]]],getpos[s[q[ind]]]); ind++; if (ind>m) { return -1; } } for (int i=ind; i<m; i++) { p[i]=0; q[i]=0; } if (!check(n,ss,m,x,y,p,q)) { return -1; } return m; } int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) { int ans=1e9; int l=0,r=m; while (l<=r) { int mm=l+(r-l)/2; int ss[n]; for (int i=0; i<n; i++) { ss[i]=s[i]; } int *pp=(int*)malloc(sizeof(int)*(unsigned int)mm), *qq=(int*)malloc(sizeof(int)*(unsigned int)mm); int t=solve(n,ss,mm,x,y,pp,qq); if (t==-1) { l=mm+1; } else { for (int i=0; i<t; i++) { p[i]=pp[i]; q[i]=qq[i]; } ans=t; r=t-1; } } assert(check(n,s,ans,x,y,p,q)); 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...