ambiguous.cpp

来自「Ulm大学2005-2006年竞赛题」· C++ 代码 · 共 46 行

CPP
46
字号
// Problem   Ambiguous Permutations
// Algorithm straight-forward
// Runtime   O(n)
// Author    Adrian Kuegel
// Date      2005.06.14

#include <iostream>
#include <fstream>
#include <cassert>
using namespace std;

#define MAXN 200000

int perm[MAXN+1];
bool used[MAXN+1];

int main() {
	ifstream in("ambiguous.in");
	int n;
	while( in >> n && n > 0 ) {
		// some asserts
		assert( n <= 100000 );
		for ( int i=1; i <= n; i++ )
			used[i] = false;

		// read the permutation
		for ( int i=1; i <= n; i++ ) {
			in >> perm[i];

			// some asserts
			assert( perm[i] >= 1 && perm[i] <= n );
			assert( !used[perm[i]] );
			used[ perm[i] ] = true;
		}
		// check the permutation
		for ( int i=1; i <= n; i++ )
			if ( perm[i] != i && perm[ perm[i] ] != i ) {
				cout << "not ";
				break;
			}
		cout << "ambiguous" << endl;
	}
	assert( n == 0 );
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?