답안 #331711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
331711 2020-11-29T16:25:15 Z ncduy0303 팬케이크 정렬 (NOI12_pancake) C++14
25 / 25
196 ms 6892 KB
#include <bits/stdc++.h>

using namespace std;

#define ar array
#define ll long long

const int MAX_N = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;

// can replace vector with bitmask to speed up
map<vector<int>,int> dist;
 
void bfs(vector<int> s) {
    queue<vector<int>> q;
    q.push(s);
    dist[s] = 0;
    while (q.size()) {
        auto u = q.front(); q.pop();
        for (int i = 0; i < u.size(); i++) {
            vector<int> v = u;
            reverse(v.begin() + i, v.end());
            if (dist.find(v) == dist.end()) {
                dist[v] = dist[u] + 1;
                q.push(v);
            }
        }
    }
}

// compute all possible answer, online bfs will get TLE
void precompute() {
    for (int i = 1; i <= 8; ++i) {
        vector<int> s(i);
        iota(s.begin(), s.end(), 0);
        reverse(s.begin(), s.end());
        bfs(s);
    }
}

void solve() {
    int n; cin >> n;
    vector<ar<int,2>> a(n);
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        a[i] = {x, i};
    }
    sort(a.begin(), a.end());
    vector<int> u(n);
    for (int i = 0; i < n; i++) u[a[i][1]] = i;
    cout << dist[u] << "\n";
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    precompute();
    int tc; cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t  << ": ";
        solve();
    }
}

Compilation message

pancake.cpp: In function 'void bfs(std::vector<int>)':
pancake.cpp:22:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |         for (int i = 0; i < u.size(); i++) {
      |                         ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 189 ms 6892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 179 ms 6784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 196 ms 6892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 180 ms 6764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 192 ms 6892 KB Output is correct