結果

問題 No.270 next_permutation (1)
ユーザー tnakao0123tnakao0123
提出日時 2016-03-31 18:45:37
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,372 bytes
コンパイル時間 730 ms
コンパイル使用メモリ 86,240 KB
実行使用メモリ 11,292 KB
最終ジャッジ日時 2024-04-10 06:50:29
合計ジャッジ時間 5,536 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
11,036 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
6,944 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 5 ms
6,940 KB
testcase_08 AC 34 ms
6,940 KB
testcase_09 AC 930 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;
}

bool equal(int n, int as[], int bs[]) {
  for (int i = 0; i < n; i++)
    if (as[i] != bs[i]) return false;
  return true;
}
  
/* 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));

  ll sum = 0;
  int t = 0;
  for (; t < k;) {
    sum += dist(n, ps, bs);
    next_permutation(ps, ps + n);
    t++;
    if (equal(n, ps, qs)) break;
  }

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

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