Submission #1247814

#TimeUsernameProblemLanguageResultExecution timeMemory
1247814kl0989e정렬하기 (IOI15_sorting)C++20
54 / 100
14 ms2100 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; } bool sorted(int n, int s[]) { bool ok=1; for (int i=0; i<n && ok; 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]; } vector<vi> lst(n,{m}); for (int i=m-1; i>=0; i--) { if (x[i]!=y[i]) { lst[x[i]].pb(i); lst[y[i]].pb(i); } } priority_queue<pi> qu; vi loc(n); int corr=0; for (int i=0; i<n; i++) { loc[s[i]]=i; if (s[i]==i) { corr++; continue; } qu.push({lst[i].back(),i}); } int ind=0; while (corr<n && ind<m) { if (x[ind]!=y[ind] && lst[x[ind]].size()>0 && lst[x[ind]].back()==ind) { lst[x[ind]].pop_back(); lst[y[ind]].pop_back(); if (s[y[ind]]!=x[ind]) { qu.push({lst[x[ind]].back(),x[ind]}); } if (s[x[ind]]!=y[ind]) { qu.push({lst[y[ind]].back(),y[ind]}); } } if (s[x[ind]]==x[ind]) { corr--; } if (s[y[ind]]==y[ind]) { corr--; } if (s[x[ind]]==y[ind]) { corr++; } if (s[y[ind]]==x[ind]) { corr++; } swap(loc[s[x[ind]]],loc[s[y[ind]]]); swap(s[x[ind]],s[y[ind]]); if (corr==n) { p[ind]=0; q[ind]=0; ind++; break; } auto [time,i]=qu.top(); qu.pop(); if (s[i]==i || time!=lst[i].back()) { if (s[x[ind]]==x[ind]) { corr--; } if (s[y[ind]]==y[ind]) { corr--; } if (s[x[ind]]==y[ind]) { corr++; } if (s[y[ind]]==x[ind]) { corr++; } swap(loc[s[x[ind]]],loc[s[y[ind]]]); swap(s[x[ind]],s[y[ind]]); continue; } p[ind]=i; q[ind]=loc[i]; if (s[p[ind]]==p[ind]) { corr--; } if (s[q[ind]]==q[ind]) { corr--; } if (s[p[ind]]==q[ind]) { corr++; } if (s[q[ind]]==p[ind]) { corr++; } swap(loc[s[p[ind]]],loc[s[q[ind]]]); swap(s[p[ind]],s[q[ind]]); ind++; } if (!check(n,ss,ind,x,y,p,q)) { return -1; } return ind; } 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)m), *qq=(int*)malloc(sizeof(int)*(unsigned int)m); 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...