結果

問題 No.2382 Amidakuji M
ユーザー tnakao0123tnakao0123
提出日時 2023-07-15 17:08:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,113 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 42,792 KB
実行使用メモリ 4,764 KB
最終ジャッジ日時 2023-10-17 03:25:14
合計ジャッジ時間 2,039 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 17 ms
4,348 KB
testcase_04 AC 30 ms
4,360 KB
testcase_05 AC 6 ms
4,348 KB
testcase_06 AC 19 ms
4,348 KB
testcase_07 AC 12 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 28 ms
4,348 KB
testcase_10 AC 40 ms
4,636 KB
testcase_11 AC 14 ms
4,348 KB
testcase_12 AC 27 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 41 ms
4,764 KB
testcase_20 AC 41 ms
4,764 KB
testcase_21 AC 41 ms
4,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2382.cc:  No.2382 Amidakuji M - yukicoder
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

typedef long long ll;

/* global variables */

int as[MAX_N], tmp[MAX_N];

/* subroutines */

ll merge_sort(int as[], int tmp[], int l, int r) { // [l, r)
  ll ic = 0;

  if (l + 1 < r) {
    int m = (l + r) / 2;
  
    ic = merge_sort(as, tmp, l, m) + merge_sort(as, tmp, m, r);

    int i = l, j = m, k = l;

    while (i < m && j < r) {
      if (as[i] <= as[j]) tmp[k++] = as[i++];
      else tmp[k++] = as[j++], ic += m - i;
    }

    while (i < m) tmp[k++] = as[i++];
    while (j < r) tmp[k++] = as[j++];

    for (int i = l; i < r; i++) as[i] = tmp[i];
  }  

  return ic;
}

/* main */

int main() {
  int n;
  ll m;
  scanf("%d%lld", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  ll c = merge_sort(as, tmp, 0, n);
  //printf("c=%lld, m=%lld\n", c, m);

  ll x = (c + m - 1) / m * m;
  if ((x - c) & 1) {
    x += m;
    if ((x - c) & 1) x = -1;
  }

  printf("%lld\n", x);

  return 0;
}
0