이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sorting.h"
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
//#define int long long
#define double long double
const int mod=1e9+7,mxn=3e5+5,lg=60,inf=1e9,minf=-1e9;
int n;
int findSwapPairs(int N, int v[], int m, int x[], int y[], int p[], int q[]) {
n=N;
int l=0,r=m;
vector<int>pos(n);
for(int i=0;i<n;i++)pos[i]=i;//position that will end up at i
vector<int>o;
for(int i=0;i<n;i++)o.pb(v[i]);
auto get=[&](int mid){
int ans=0,cur=0;
vector<int>rev(n);
for(int i=0;i<n;i++)rev[v[i]]=i;
vector<int>pos2(n);//where we will end up standing at i
for(int i=0;i<n;i++)pos2[i]=i;
for(int i=mid-1;i>=0;i--)swap(pos2[x[i]],pos2[y[i]]);
for(int i=0;i<n;i++)pos[pos2[i]]=i;
vector<pii>what(mid);
for(int i=0;i<mid;i++){
swap(pos[pos2[x[i]]],pos[pos2[y[i]]]);
swap(pos2[x[i]],pos2[y[i]]);
swap(rev[v[x[i]]],rev[v[y[i]]]);
swap(v[x[i]],v[y[i]]);
while(cur<n&&pos[cur]==rev[cur])cur++;
if(cur<n){
what[i]={pos[cur],rev[cur]};
swap(rev[v[what[i].f]],rev[v[what[i].s]]);
swap(v[what[i].f],v[what[i].s]);
cur++;
ans++;
}
else what[i]={0,0};
}
while(cur<n&&pos[cur]==rev[cur])cur++;
return make_pair((cur>=n),what);
};
pair<int,vector<pii>>ans{inf,{}};
while(l<=r){
int mid=l+(r-l)/2;
int cnt=0;
pair<int,vector<pii>>bru=get(mid);
if(bru.f)r=mid-1,ans=min(ans,{mid,bru.s});
else l=mid+1;
for(int i=0;i<n;i++)v[i]=o[i];
}
for(int i=0;i<ans.f;i++)p[i]=ans.s[i].f,q[i]=ans.s[i].s;
return ans.f;
}/*
int32_t main(){
int n;cin>>n;
int v[n];
for(int i=0;i<n;i++)cin>>v[i];
int m;cin>>m;
int x[m],y[m];
for(int i=0;i<m;i++)cin>>x[i]>>y[i];
int p[m],q[m];
int g=findSwapPairs(n,v,m,x,y,p,q);
cout<<g<<'\n';
for(int i=0;i<g;i++){
swap(v[x[i]],v[y[i]]);
swap(v[p[i]],v[q[i]]);
}
for(int i=0;i<n;i++)cout<<v[i]<<" ";
cout<<'\n';
}*/
/*
observation:
each starting element will end up somewhere after swapping
each element have distinct path to the end position
we can swap element to swap their path
swap can be made in any order?
if the min num of swap <= num of action
then its doable
min num of swap can increase by at most 1 per action (swapping it back to same pos)
so num of action-min num of swap is monotone
we can do bs? and check o(n)?
finding min num of swap: greedy?
*/
컴파일 시 표준 에러 (stderr) 메시지
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:76:7: warning: unused variable 'cnt' [-Wunused-variable]
76 | int cnt=0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |