Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
list($n, $k, $x) = explode(" ",trim(fgets(STDIN)));
$start = range(1, $n); // 初期状態
$manipulate = []; // 後ろからの操作があるので入力の都度処理ではなく記録
for($i = $k;$i;$i--)
{
$manipulate[] = explode(" ",trim(fgets(STDIN)));
}
$goal = explode(" ",trim(fgets(STDIN))); // 最後の状態
// 交換のシミュレート
$start = exchange($start, array_slice($manipulate,0,$x - 1));
$goal = exchange($goal, array_reverse(array_slice($manipulate,$x)));
// 差を調べる
$diff = diff($start, $goal);
sort($diff); // 出力制約
echo (implode(" ",$diff)).PHP_EOL;
/**
* @param base 操作の元となる配列
* @param acts 操作手順
* @return 操作結果の配列
*/
function exchange($base, $acts)
{
foreach($acts as $act)
{
$a = $act[0]-1; //num to index
$b = $act[1]-1; //num to index
$tmp = $base[$b];
$base[$b] = $base[$a];
$base[$a] = $tmp;
}
return $base;
}
function diff($a, $b)
{
$res=[];
$len = count($a);
for($i=0;$i < $len;$i++)
{
if($a[$i] != $b[$i])
{
$res[] = $i+1; //index to num
}
}
return $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment