SRM448 Div2 Level.2
左・真ん中・右にカードのデッキがあって、とあるアルゴリズムに従ってカードをシャッフルする。
それが終わったあと真ん中のデッキの一番上のカードは何かという問題。
総当たりのコードは簡単に書ける。が、大きい値を入れられると当然タイムオーバー。
using System; using System.Collections.Generic; using System.Text; public class TheCardShufflingDivTwo { public int shuffle(int n, int m) { Stack<int> left = new Stack<int>(); Stack<int> right = new Stack<int>(); Stack<int> main = new Stack<int>(); for (int i = n; i >= 1; i--) { main.Push(i); } int count = 0; while (count < m) { int j = 1; while (main.Count != 0) { if (j % 2 == 0) { right.Push(main.Pop()); } else { left.Push(main.Pop()); } j++; } while (left.Count != 0) { main.Push(left.Pop()); } while (right.Count != 0) { main.Push(right.Pop()); } count++; } return main.Pop(); } }
正答者のコードみたら数列の法則見つけて簡単に解いてた。
ぼかあセンスがないね。