#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<int, vi> piv;
typedef pair<vi, pii> pvp;
typedef long long ll;
int n, A[1010];
map<vi, int> D;
map<vi, pvp> prv;
void dijk(vi &st){
priority_queue<piv, vector<piv>, greater<piv>> Q;
Q.push({0, st}), D[st]=0;
while(!Q.empty()){
vi v; int d; tie(d,v)=Q.top(); Q.pop();
if(D[v]!=d) continue;
// for(int x:v) cout<<x<<' ';
// cout<<": "<<d<<'\n';
for(int i=1; i<=n; i++){
vi now=v;
for(int j=i+1; j<=n; j++){
swap(now[j-2], now[j-1]);
if(D.find(now)!=D.end() && D[now]<=d+i+j) continue;
D[now]=d+i+j; Q.push({D[now], now});
prv[now]={v, {i,j}};
}
}
for(int i=1; i<=n; i++){
vi now=v;
for(int j=1; j<i; j++){
swap(now[j-1], now[j]);
if(D.find(now)!=D.end() && D[now]<=d+i+j) continue;
D[now]=d+i+j; Q.push({D[now], now});
prv[now]={v, {i,j}};
}
}
}
vi ed; for(int i=n-1; i>=0; i--) ed.push_back(i);
stack<pii> stk;
while(ed!=st){
stk.push({prv[ed].second});
ed=prv[ed].first;
}
cout<<stk.size()<<'\n';
while(!stk.empty()){ int a,b; tie(a,b)=stk.top(); stk.pop(); cout<<a<<' '<<b<<'\n'; }
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>n;
for(int i=1; i<=n; i++) cin>>A[i];
vector<int> X; for(int i=1; i<=n; i++) X.push_back(A[i]);
sort(X.begin(), X.end());
for(int i=1; i<=n; i++) A[i]=lower_bound(X.begin(), X.end(), A[i])-X.begin();
X.clear();
for(int i=1; i<=n; i++) X.push_back(A[i]);
dijk(X);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
2040 KB |
Output is correct |
2 |
Execution timed out |
1082 ms |
35768 KB |
Time limit exceeded |
3 |
Execution timed out |
1079 ms |
53684 KB |
Time limit exceeded |
4 |
Execution timed out |
1053 ms |
132096 KB |
Time limit exceeded |
5 |
Execution timed out |
1059 ms |
132096 KB |
Time limit exceeded |
6 |
Execution timed out |
1095 ms |
132096 KB |
Time limit exceeded |
7 |
Execution timed out |
1086 ms |
132096 KB |
Time limit exceeded |
8 |
Execution timed out |
1040 ms |
132096 KB |
Time limit exceeded |
9 |
Execution timed out |
1110 ms |
132096 KB |
Time limit exceeded |
10 |
Execution timed out |
1107 ms |
132096 KB |
Time limit exceeded |