#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;
struct dum {
int c; vi v;
bool operator < (const dum &d) const {
return c>d.c;
}
};
void dijk(vi &st){
vi ed; for(int i=n-1; i>=0; i--) ed.push_back(i);
priority_queue<dum> Q;
Q.push({0, st}), D[st]=0;
while(!Q.empty()){
vi v=Q.top().v; int d=Q.top().c; Q.pop();
if(D[v]!=d) continue;
if(v==ed) break;
// 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=2; 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}};
}
}
}
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 |
50 ms |
1784 KB |
Output is correct |
2 |
Execution timed out |
1083 ms |
28340 KB |
Time limit exceeded |
3 |
Execution timed out |
1059 ms |
36136 KB |
Time limit exceeded |
4 |
Execution timed out |
1018 ms |
132096 KB |
Time limit exceeded |
5 |
Execution timed out |
1060 ms |
132096 KB |
Time limit exceeded |
6 |
Execution timed out |
1063 ms |
132096 KB |
Time limit exceeded |
7 |
Execution timed out |
1029 ms |
132096 KB |
Time limit exceeded |
8 |
Execution timed out |
1101 ms |
132096 KB |
Time limit exceeded |
9 |
Execution timed out |
1096 ms |
132096 KB |
Time limit exceeded |
10 |
Execution timed out |
1117 ms |
132096 KB |
Time limit exceeded |