結果

問題 No.366 ロボットソート
ユーザー nanophoto12nanophoto12
提出日時 2016-05-04 23:00:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,299 bytes
コンパイル時間 759 ms
コンパイル使用メモリ 89,368 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 15:14:35
合計ジャッジ時間 1,648 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 4 ms
4,380 KB
testcase_22 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
using namespace std;

#define ll long long int
#define tlll tuple<ll,ll,ll>
#define pii pair<ll,ll>

tlll params[200];

int main()
{
    int n, k;
    cin >> n >> k;
    vector<pii> vs(n);
    for(int i = 0;i < n;i++)
    {
        ll v;
        cin >> v;
        vs[i] = make_pair(v, i);
    }
    sort(vs.begin(), vs.end());
    vector<ll> vss(n);
    for(int i = 0;i < n;i++)
    {
        vss[vs[i].second] = i;
    }
    vector<vector<ll>> tokens(k);
    for(int i = 0;i < n;i++)
    {
        if((vss[i] % k) != (i % k))
        {
            cout << -1 << endl;
            return 0;
        }
        tokens[i%k].push_back(vss[i]);
    }
    ll count = 0;
    for(auto& e : tokens)
    {
        for(int i = e.size() - 1;i >= 0;i--)
        {
            for(int j = e.size() - 1;j >= 0;j--)
            {
                int k = j - 1;
                if(k >= 0)
                {
                    if(e[k] > e[j])
                    {
                        swap(e[k], e[j]);
                        count++;
                    }
                }
            }
        }
    }
    cout << count << endl;
    return 0;
}
0