답안 #666270

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
666270 2022-11-28T02:50:18 Z vuavisao 팬케이크 정렬 (NOI12_pancake) C++14
20 / 25
1000 ms 1640 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
using namespace std;

const int N = 8 + 1;

bool cakeee(int* val);
bool special_case_zero();
bool special_case_one();
bool special_case_two();
void bfs_make_cakeee();
void solve();

int n;
int a[N];

struct state {
    int val[N];
    state() {};

    state flip(int idx) {
        state res;
        for(int i = 0; i <= n; ++ i) res.val[i] = this -> val[i];
        reverse(res.val + idx, res.val + 1 + n);
        return res;
    }

    bool operator < (const state& other) const {
        for(int i = 1; i <= n; ++ i)
            if(this -> val[i] != other.val[i]) return this -> val[i] < other.val[i];
        return false;
    }
};

void bfs_make_cakeee() {
    state first;
    for(int i = 0; i <= n; ++ i) first.val[i] = a[i];
    queue<state> q = {}; q.push(first);
    map<state, int> mp = {}; mp[first] = 0;
    while(q.empty() ^ 1) {
        state u = q.front(); q.pop();
        if(mp[u] >= 2 * n) continue;

        for(int i = 1; i <= n; ++ i) {
            state nxt = u.flip(i);
            if(mp.find(nxt) != mp.end()) continue;
            mp[nxt] = mp[u] + 1;
            if(cakeee(nxt.val)) {
                cout << mp[nxt];
                return;
            }
            q.push(nxt);
        }
    }
}

void solve() {
    cin >> n;
    for(int i = 1; i <= n; ++ i) cin >> a[i];
    a[0] = 1000000000;
    if(special_case_zero()) {
        cout << 0;
        return;
    }
    if(special_case_one()) {
        cout << 1;
        return;
    }
    if(special_case_two()) {
        cout << 2;
        return;
    }
    bfs_make_cakeee();
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fopen("Cake.inp", "r")) {
        freopen("Cake.inp", "r", stdin);
        freopen("Cake.out", "w", stdout);
    }
    int t; cin >> t;
    while(t--) {
        solve();
        cout << '\n';
    }
    return 0;
}

bool special_case_zero() {
    return cakeee(a);
}

bool special_case_one() {
    int tmp[N];
    for(int i = 1; i <= n; ++ i) {
        for(int j = 0; j <= n; ++ j) tmp[j] = a[j];
        reverse(tmp + i, tmp + n + 1);
        if(cakeee(tmp)) return true;
    }
    return false;
}

bool special_case_two() {
    int tmp[N];
    for(int i = 1; i <= n; ++ i) {
        for(int j = 1; j <= n; ++ j) {
            for(int k = 0; k <= n; ++ k) tmp[k] = a[k];
            reverse(tmp + i, tmp + n + 1);
            reverse(tmp + j, tmp + n + 1);
            if(cakeee(tmp)) return true;
        }
    }
    return false;
}

bool cakeee(int* val) {
    for(int i = 1; i <= n; ++ i)
        if(val[i] > val[i - 1]) return false;
    return true;
}

/// Code by vuavisao

Compilation message

pancake.cpp: In function 'int32_t main()':
pancake.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |         freopen("Cake.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pancake.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |         freopen("Cake.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 1468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1066 ms 1640 KB Time limit exceeded
2 Halted 0 ms 0 KB -