結果

問題 No.366 ロボットソート
ユーザー siguma323siguma323
提出日時 2016-04-30 01:11:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,664 bytes
コンパイル時間 800 ms
コンパイル使用メモリ 88,056 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-28 15:13:40
合計ジャッジ時間 1,709 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <numeric>
#include <string>
#include <vector>
#include <iostream>
#include <queue>
#include <utility>
#include <cmath>
#include <bitset>

using namespace std;

using ull = unsigned long long;

bool check(int index1,int index2,int k)
{
    int sub = abs(index1 - index2);
    if(sub % k == 0) return true;
    else return false;
}

void bubble_check(vector<int>& x,int& count)
{
    for(int i =0; i < x.size() -1; ++i)
    {
        for(int j = x.size()-1; j > i; --j)
        {
            if(x.at(j-1) > x.at(j))
            {
                swap(x.at(j),x.at(j-1));
                ++count;
            }
        }
    }
}


int main()
{
    ull n,k;
    cin >> n >> k;
    vector<ull> a(n);
    int count = 0;

    for(auto&& x : a)
    {
        cin >> x;
    }

    vector<ull> sorted_a = a;
    sort(sorted_a.begin(),sorted_a.end());

    if(k >= n)
    {
        if(sorted_a == a)
        {
            cout << 0 << endl;
            return 0;
        }
        else
        {
            cout << -1 << endl;
            return 0;
        }
    }

    ull i = 0;
    vector<vector<int>> hoge(k);

    for(int i =0; i < n; ++i)
    {
        auto iter1 = find(a.begin(), a.end(),a.at(i));
        int index1 = distance(a.begin(), iter1);
        auto iter2 = find(sorted_a.begin(), sorted_a.end(),a.at(i));
        int index2 = distance(sorted_a.begin(), iter2);

        if(!check(index1,index2,k))
        {
            cout << -1 << endl;
            return 0;
        }
        hoge.at(i % k).emplace_back(a.at(i));
    }

    for(auto&& x: hoge)
    {
        bubble_check(x,count);
    }

    cout << count << endl;



}
0