結果

問題 No.366 ロボットソート
ユーザー Mister
提出日時 2020-04-14 01:56:12
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,084 bytes
コンパイル時間 1,251 ms
コンパイル使用メモリ 91,808 KB
最終ジャッジ日時 2025-01-09 18:21:59
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>

template <class T>
std::map<T, int> compress(std::vector<T>& v) {
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());

    std::map<T, int> rev;
    for (int i = 0; i < (int)v.size(); ++i) rev[v[i]] = i;
    return rev;
}

void solve() {
    int n, k;
    std::cin >> n >> k;

    std::vector<int> xs(n);
    for (auto& x : xs) std::cin >> x;

    {
        auto nxs = xs;
        auto revx = compress(nxs);
        for (auto& x : xs) x = revx[x];
    }

    int ans = 0;
    std::vector<std::vector<int>> yss(k);
    for (int i = 0; i < n; ++i) {
        auto x = xs[i];

        if (x % k != i % k) {
            std::cout << -1 << std::endl;
            return;
        }
        x /= k;

        auto& ys = yss[i % k];
        for (auto y : ys) {
            if (y > x) ++ans;
        }

        ys.push_back(x);
    }

    std::cout << ans << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0