답안 #64027

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
64027 2018-08-03T08:26:52 Z 노영훈(#1868) Ranklist Sorting (BOI07_sorting) C++11
0 / 100
1000 ms 132096 KB
#include <bits/stdc++.h>
using namespace std;
typedef vector<uint_fast8_t> 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;
    assert(n<=10);
    for(int i=1; i<=n; i++) cin>>A[i];
    vi 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 Execution timed out 1083 ms 132096 KB Time limit exceeded
2 Execution timed out 1068 ms 132096 KB Time limit exceeded
3 Execution timed out 1076 ms 132096 KB Time limit exceeded
4 Runtime error 2 ms 132096 KB Execution killed with signal 11 (could be triggered by violating memory limits)
5 Runtime error 3 ms 132096 KB Execution killed with signal 11 (could be triggered by violating memory limits)
6 Runtime error 3 ms 132096 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 3 ms 132096 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Runtime error 3 ms 132096 KB Execution killed with signal 11 (could be triggered by violating memory limits)
9 Runtime error 3 ms 132096 KB Execution killed with signal 11 (could be triggered by violating memory limits)
10 Runtime error 3 ms 132096 KB Execution killed with signal 11 (could be triggered by violating memory limits)