結果

問題 No.270 next_permutation (1)
ユーザー tnakao0123tnakao0123
提出日時 2016-03-31 18:57:33
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,317 bytes
コンパイル時間 656 ms
コンパイル使用メモリ 86,228 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-04-10 06:51:01
合計ジャッジ時間 5,167 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,756 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 4 ms
6,944 KB
testcase_08 AC 27 ms
6,940 KB
testcase_09 AC 715 ms
6,940 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 270.cc: No.270 next_permutation (1) - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_K = 100000;

/* typedef */

typedef long long ll;

/* global variables */

int ps[MAX_N], qs[MAX_N], bs[MAX_N];

/* subroutines */

ll dist(int n, int as[], int bs[]) {
  ll sum = 0;
  for (int i = 0; i < n; i++) sum += abs(as[i] - bs[i]);
  return sum;
}

/* main */

int main() {
  int n, k;
  cin >> n >> k;

  for (int i = 0; i < n; i++) cin >> ps[i];
  for (int i = 0; i < n; i++) cin >> bs[i];
  memcpy(qs, ps, sizeof(ps));

  int fn = 1;
  for (int i = 1; i <= n; i++) {
    fn *= i;
    if (fn > k) {
      fn = k;
      break;
    }
  }
  
  ll sum = 0;
  for (int i = 0; i < fn; i++) {
    sum += dist(n, ps, bs);
    next_permutation(ps, ps + n);
  }

  if (fn < k) {
    sum *= k / fn;
    k %= fn;
    while (k--) {
      sum += dist(n, ps, bs);
      next_permutation(ps, ps + n);
    }
  }

  printf("%lld\n", sum);
  return 0;
}
0