結果

問題 No.270 next_permutation (1)
ユーザー y_mazuny_mazun
提出日時 2015-08-25 15:23:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 959 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 48,560 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 06:59:52
合計ジャッジ時間 1,369 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cstdlib>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

typedef long long ll;

#include <queue>
#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }

#include <set>

using namespace std;

struct t{
  int i;
  t(){}
};

int p[100000];
int b[100000];
t idx[100000];
ll ans;
int rest;

bool operator < (t i, t j){
  return p[i.i] < p[j.i];
}

void swap(t i, t j){
  ans -= (ll)rest * std::abs(b[i.i] - p[i.i]);
  ans -= (ll)rest * std::abs(b[j.i] - p[j.i]);
  swap(p[i.i], p[j.i]);
  swap(i.i, j.i);
  ans += (ll)rest * std::abs(b[i.i] - p[i.i]);
  ans += (ll)rest * std::abs(b[j.i] - p[j.i]);
}

int main(){
  const int n = getInt();
  const int k = getInt();

  REP(i,n) p[i] = getInt();
  REP(i,n) b[i] = getInt();
  REP(i,n) idx[i].i = i;
  REP(i,n) ans += (ll)k * std::abs(p[i] - b[i]);

  REP(i,k-1){
    rest = k - 1 - i;
    next_permutation(idx, idx + n);
  }

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