結果
| 問題 |
No.270 next_permutation (1)
|
| コンテスト | |
| ユーザー |
izuru_matsuura
|
| 提出日時 | 2016-11-15 18:34:40 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 30 ms / 2,000 ms |
| コード長 | 1,492 bytes |
| コンパイル時間 | 1,568 ms |
| コンパイル使用メモリ | 151,440 KB |
| 実行使用メモリ | 10,608 KB |
| 最終ジャッジ日時 | 2024-06-12 05:05:31 |
| 合計ジャッジ時間 | 2,756 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 |
ソースコード
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) {
stderr.writeln(arg);
}
int size(T)(in T s) {
return cast(int)s.length;
}
long calc(in long[] a, in long[] b) {
assert(a.size == b.size);
long ans = 0;
foreach (i; 0 .. a.size) {
ans += abs(a[i] - b[i]);
}
return ans;
}
void main() {
int N, K; readf("%d %d\n", &N, &K);
auto P = readln.chomp.split(" ").map!(to!long).array;
auto B = readln.chomp.split(" ").map!(to!long).array;
if (K == 0) {
writeln(0);
return;
}
if (N <= 10) {
long ans = 0;
do {
ans += calc(P, B);
K--;
nextPermutation(P);
} while (K > 0);
writeln(ans);
return;
}
long[] yabaikeesu;
for (long i = N - 9; i <= N; i++) {
yabaikeesu ~= i;
}
long ans = 0;
long base = calc(P[0 .. $ - 10], B[0 .. $ - 10]);
auto p = P[$ - 10 .. $];
auto b = B[$ - 10 .. $];
do {
ans += base + calc(p, b);
nextPermutation(p);
if (p == yabaikeesu) {
auto wp = P[0 .. $ - 10] ~ p;
nextPermutation(wp);
base = calc(wp[0 .. $ - 10], B[0 .. $ - 10]);
p = wp;
} else {
K--;
}
} while (K > 0);
writeln(ans);
}
izuru_matsuura