Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@papinianus
Created October 5, 2016 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save papinianus/54b7e24eea344cf31f2fac0fa4141d85 to your computer and use it in GitHub Desktop.
Save papinianus/54b7e24eea344cf31f2fac0fa4141d85 to your computer and use it in GitHub Desktop.
<?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