結果

問題 No.270 next_permutation (1)
ユーザー Kmcode1Kmcode1
提出日時 2015-08-21 23:14:25
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,264 bytes
コンパイル時間 620 ms
コンパイル使用メモリ 89,696 KB
実行使用メモリ 12,484 KB
最終ジャッジ日時 2023-09-25 15:23:55
合計ジャッジ時間 5,234 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
#include<functional>
#include<climits>
#include<cassert>
#include<iterator>
using namespace std;

#define MAX 100002
int n;
int k;
long long int p[MAX];
long long int b[MAX];
bool visited[MAX];
long long int dist = 0;
long long int summ = 0;
bool flag[MAX];
inline void dfs(int a){
	if (a == n){
		k--;
		summ += dist;
		return;
	}
	int be = 1;
	if (!visited[a]){
		be = p[a];
		visited[a] = true;
	}
	for (int i = be; i <= n; i++){
		if (flag[i]){
			continue;
		}
		flag[i] = true;
		dist += (long long int)(abs(i - b[a]));
		dfs(a + 1);
		dist -= (long long int)(abs(i - b[a]));
		flag[i] = false;
		if (k == 0){
			return;
		}
	}
}
int main(){
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; i++){
		scanf("%d", &p[i]);
	}
	for (int i = 0; i < n; i++){
		scanf("%d", &b[i]);
	}
	int countt3 = 0;
	while (k){
		dfs(0);
		countt3++;
		if (countt3 > 2){
			break;
		}
	}
	printf("%lld\n", summ);
	return 0;
}
0