제출 #1321501

#제출 시각아이디문제언어결과실행 시간메모리
1321501duckindogSecret Permutation (RMI19_permutation)C++20
100 / 100
2568 ms432 KiB
#include <bits/stdc++.h>

#ifndef LOCAL
#include "permutation.h"
#endif

using namespace std;

#ifdef LOCAL
int CNTA = 0;
vector<int> GLOBAL;
void answer(vector<int> P) { 
    CNTA += 1;
    P.insert(P.begin(), 0);
    assert(P == GLOBAL);
}
int query(vector<int> V) { 
    int ret = 0;
    for (int i = 0; i < (int)V.size() - 1; ++i) { 
        ret += abs(GLOBAL[V[i]] - GLOBAL[V[i + 1]]);
    }
    return ret;
}
#endif

mt19937 rng(49853495);

void solve(int N) { 
    vector<int> v(N); iota(v.begin(), v.end(), 1);
    shuffle(v.begin(), v.end(), rng);
    
    vector<int> value(N);
    for (int i = 0; i < N; ++i) { 
        value[i] = query(v);
        v.push_back(v[0]);
        v.erase(v.begin());
    }
    
    int total = accumulate(value.begin(), value.end(), 0);
    total /= (N - 1);

    vector<pair<int, int>> nxt(N + 1);
    for (int i = 0; i < N; ++i) { 
        int p = (i - 1 + N) % N;
        nxt[v[p]] = {v[i], total - value[i]};
    }

    vector<int> answerV;
    
    vector<bool> mk(N + 1, true);
    vector<int> arr(N + 1);
    function<void(int)> recur = [&](int u) { 
        if (answerV.size()) return;
        if (u == v.back()) {
            auto [v, w] = nxt[u];
            if (abs(arr[u] - arr[v]) == w) {
                answerV.assign(arr.begin() + 1, arr.end());
            }
            return;
        }

        auto [v, w] = nxt[u];
        for (int dir = -1; dir <= 1; dir += 2) {
            int valueV = arr[u] + dir * w;
            if (valueV < 1 || valueV > N) continue;
            if (!mk[valueV]) continue;
            
            mk[valueV] = false;
            arr[v] = valueV;
            recur(v);
            mk[valueV] = true;
        }
    };

    for (int st = 1; st <= N; ++st) { 
        mk[st] = false;
        arr[v[0]] = st;
        recur(v[0]);
        mk[st] = true;
    }

    answer(answerV);
}

#ifdef LOCAL
int32_t main() { 
    cin.tie(0)->sync_with_stdio(0);

    int N; cin >> N;
    GLOBAL.resize(N);
    for (auto& x : GLOBAL) cin >> x;
    GLOBAL.insert(GLOBAL.begin(), 0);

    solve(N);
    assert(CNTA == 1);
}
#endif

컴파일 시 표준 에러 (stderr) 메시지

stub.cpp: In function 'int query(int*)':
stub.cpp:15:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |   fscanf(stdin, "%d", &x);
      |   ~~~~~~^~~~~~~~~~~~~~~~~
stub.cpp: In function 'int main(int, char**)':
stub.cpp:48:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |   fscanf(stdin, "%d", &N);
      |   ~~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...