結果

問題 No.326 あみだますたー
ユーザー koyumeishikoyumeishi
提出日時 2015-12-19 09:38:08
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,626 bytes
コンパイル時間 718 ms
コンパイル使用メモリ 92,580 KB
実行使用メモリ 5,252 KB
最終ジャッジ日時 2023-10-14 14:15:40
合計ジャッジ時間 4,171 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 8 ms
4,352 KB
testcase_09 AC 8 ms
4,352 KB
testcase_10 AC 8 ms
4,352 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <functional>
#include <set>
#include <ctime>
#include <random>
using namespace std;
template<class T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;}
template<class T> istream& operator , (istream& is, T& val){ return is >> val;}
template<class T> ostream& operator << (ostream& os, vector<T>& vec){for(int i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"\n":" ");return os;}

#include <cassert>

int main(){
	int n,k;
	cin >> n >> k;
	vector<int> x(k),y(k);
	for(int i=0; i<k; i++){
		cin >> x[i] >> y[i];
		x[i]--;
		y[i]--;
	}
	vector<int> a(n);
	cin >> a;

	vector<int> b(n);
	iota(b.begin(), b.end(), 0);

	for(int i=0; i<k; i++){
		swap(b[x[i]], b[y[i]]);
	}
	vector<int> d(n);
	for(int i=0; i<n; i++){
		d[b[i]] = i;
		a[i]--;
	}

	vector<int> p,q;
	for(int i=0; i<n; i++){
		while(d[a[i]] != i){
			int u = d[a[i]];
			int v = u-1;
			p.push_back(v);
			q.push_back(u);
			swap(b[u], b[v]);
			d[b[v]] = v;
			d[b[u]] = u;
		}
	}
	assert(0<=p.size());
	assert(p.size() <= 6000);

	cout << p.size() << endl;
	for(int i=0; i<p.size(); i++){
		
		assert(p[i]+1 == q[i]);
		assert(1<=p[i]+1);
		assert(q[i]+1<=n);

		cout << p[i]+1 << " " << q[i]+1 << endl;
	}

	assert(b == a);

	vector<int> test(n);
	iota(test.begin(), test.end(), 0);
	for(int i=0; i<k; i++){
		swap(test[x[i]], test[y[i]]);
	}
	for(int i=0; i<p.size(); i++){
		swap(test[p[i]], test[q[i]]);
	}

	assert(a==test);

	return 0;
}
0