結果

問題 No.241 出席番号(1)
コンテスト
ユーザー ciel
提出日時 2015-07-12 03:08:40
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 863 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 394 ms
コンパイル使用メモリ 81,916 KB
最終ジャッジ日時 2026-05-24 11:29:09
合計ジャッジ時間 992 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:7:33: error: 'time' was not declared in this scope
    7 | mt19937_64 engine((unsigned int)time(NULL)^(getpid()<<16));
      |                                 ^~~~
main.cpp:6:1: note: 'time' is defined in header '<ctime>'; this is probably fixable by adding '#include <ctime>'
    5 | #include <unistd.h>
  +++ |+#include <ctime>
    6 | using namespace std;

ソースコード

diff #
raw source code

#include <vector>
#include <random>
#include <algorithm>
#include <cstdio>
#include <unistd.h>
using namespace std;
mt19937_64 engine((unsigned int)time(NULL)^(getpid()<<16));

bool validate(const vector<int>&a,const vector<int>&r){
	vector<bool>f(r.size());
	for(int i=0;i<r.size();i++){
		if(r[i]<0 || r.size()<=r[i])return false;
		if(r[i]==a[i])return false;
		if(f[r[i]])return false;
		f[r[i]]=true;
	}
	return true;
}

int main(){
	int N;
	scanf("%d",&N);
	vector<int>a(N),r(N);
	for(int i=0;i<N;i++)scanf("%d",&a[i]),r[i]=i;
	if(0<=a[0]&&a[0]<N){
		int i=0;
		for(;i<N;i++)if(a[0]!=a[i])break;
		if(i==N){
			puts("-1");
			return 0;
		}
	}
	uniform_int_distribution<int> distribution(0, N-1);
	for(;;){
		int x=distribution(engine);
		int y=distribution(engine);
		swap(r[x],r[y]);
		if(validate(a,r))break;
	}
	for(int i=0;i<N;i++)printf("%d\n",r[i]);
}
0