結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー koyumeishikoyumeishi
提出日時 2014-11-28 08:48:59
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,453 bytes
コンパイル時間 1,174 ms
コンパイル使用メモリ 67,924 KB
実行使用メモリ 7,864 KB
最終ジャッジ日時 2023-09-01 22:39:29
合計ジャッジ時間 18,459 ms
ジャッジサーバーID
(参考情報)
judge14 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 199 ms
4,380 KB
testcase_12 AC 166 ms
4,380 KB
testcase_13 AC 104 ms
4,380 KB
testcase_14 AC 254 ms
4,376 KB
testcase_15 AC 23 ms
4,380 KB
testcase_16 AC 24 ms
4,376 KB
testcase_17 AC 31 ms
4,380 KB
testcase_18 AC 36 ms
4,376 KB
testcase_19 AC 41 ms
4,376 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 54 ms
4,376 KB
testcase_23 AC 35 ms
4,380 KB
testcase_24 AC 442 ms
4,376 KB
testcase_25 AC 192 ms
4,376 KB
testcase_26 AC 130 ms
4,376 KB
testcase_27 AC 2,032 ms
4,376 KB
testcase_28 AC 1,229 ms
4,380 KB
testcase_29 AC 1,123 ms
4,376 KB
testcase_30 AC 914 ms
4,376 KB
testcase_31 AC 904 ms
4,376 KB
testcase_32 AC 887 ms
4,380 KB
testcase_33 TLE -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

//想定TLE解法 ループを見つけるまで回す
//1.あみだくじを1回だけシミュレートして置換先を決定し、元の列に戻るまでループを回す。多分TLE

//n<=100のため最大ケースは232,792,560 = 16*9*5*7*11*13*17*19となる。多分。オーバーフローはしないはず


#include <iostream>
#include <vector>
#include <map>
#include "assert.h"
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <sstream>

using namespace std;

#define MAX_N 100
#define MAX_K 1000

//find the loop
int solve_loop(const vector<int> &v){
	int ret = -1;
	
	int n = v.size();

	vector<int> pos(n);
	for(int i=0; i<n; i++) pos[i] = i;
	
	vector<int> next(n);
	
	for(int i=0; ; i++){
		for(int j=0; j<n; j++){
			next[j] = pos[ v[j] ];
		}

		bool loop=true;
		for(int j=0; j<n; j++){
			if(next[j] != j){
				loop = false;
				break;
			}
		}
		if(loop){
			ret = i+1;
			break;
		}
		
		swap(pos, next);
	}

	return ret;
}

int main(){
	int N;
	cin >> N;
	assert(2<=N && N<=MAX_N);
	
	int K;
	cin >> K;
	assert(0<=K && K<=MAX_K);
	
	
	vector<int> v(N);
	for(int i=0; i<N; i++){
		v[i] = i;
	}
	for(int i=0; i<K; i++){
		int x,y;
		cin >> x >> y;
		assert(1<=x && x<= N);
		assert(1<=y && y<= N);
		assert(x<y);
		assert(x+1 == y);
		
		x--;
		y--;
		
		swap( v[x], v[y] );
	}

	cout << solve_loop(v) << endl;
	
	return 0;
}
0