結果

問題 No.270 next_permutation (1)
ユーザー Kmcode1Kmcode1
提出日時 2015-08-21 23:20:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 1,376 bytes
コンパイル時間 816 ms
コンパイル使用メモリ 101,056 KB
実行使用メモリ 14,608 KB
最終ジャッジ日時 2023-08-22 23:21:52
合計ジャッジ時間 2,283 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 7 ms
4,376 KB
testcase_02 AC 9 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 8 ms
4,380 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 8 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 16 ms
4,676 KB
testcase_10 AC 134 ms
14,608 KB
testcase_11 AC 145 ms
14,520 KB
testcase_12 AC 143 ms
14,548 KB
testcase_13 AC 127 ms
14,156 KB
testcase_14 AC 125 ms
14,072 KB
権限があれば一括ダウンロードができます

ソースコード

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>
#include<unordered_set>
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];
set<int> s;
set<int>::iterator ite;
inline void dfs(int a){
	if (a == n){
		k--;
		summ += dist;
		return;
	}
	int be = 1;
	ite = s.begin();
	if (!visited[a]){
		be = p[a];
		ite = s.lower_bound(be);
		visited[a] = true;
	}
	for (; ite != s.end(); ite++){
		int i = (*ite);
		s.erase(ite);
		dist += (long long int)(abs(i - b[a]));
		dfs(a + 1);
		dist -= (long long int)(abs(i - b[a]));
		s.insert(i);
		ite = s.lower_bound(i);
		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]);
	}
	for (int i = 1; i <= n; i++){
		s.insert(i);
	}
	int countt3 = 0;
	while (k){
		dfs(0);
	}
	printf("%lld\n", summ);
	return 0;
}
0