結果

問題 No.366 ロボットソート
ユーザー siguma323
提出日時 2016-04-30 01:11:10
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,664 bytes
コンパイル時間 1,014 ms
コンパイル使用メモリ 89,908 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-29 17:47:26
合計ジャッジ時間 1,825 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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