TopCoder SRM 344 Div1 Easy VolleyballTournament
問題
バレーボールのマッチは、3セットを先に取ったほうの勝ちである。
ゲームの結果、合計で
wonMatchesのマッチで勝ち、
lostMatchesのマッチで負け、
取ったセットの数を合計すると、wonSetsになり、
取られたセットの数を合計するとlostSetsになるとき、
試合結果を(取ったセット)-(取られたセット)の形で、スペースで区切って出力せよ。
ただし、辞書順で最小になるように並べよ。
複数の結果がありえる場合はAMBIGUITYを出力せよ。
制約条件
wonMatches,lostMatches≦100
wonSets,lostSets≦500
方針
勝ちでwonSetsを3消費し、負けでlostSetsを3消費する。
残りの分については、負け試合にwonSetsを入れて、勝った試合にlostSetsを入れる。
負け試合が複数あって、wonSetsが2以上(負け試合の数)*2-2以下だったら、複数通りのwonSetsの分配の仕方があるのでAMBIGUITY.
lostSetsについても同様。
ソースコード
class VolleyballTournament { public: string reconstructResults(int wonMatches, int lostMatches, int wonSets, int lostSets) { int won=wonSets-wonMatches*3, lost=lostSets-lostMatches*3; if(lostMatches>1&&1<won&&won<2*lostMatches-1|| wonMatches>1&&1<lost&&lost<2*wonMatches-1)return "AMBIGUITY"; vs ans; rep(i,wonMatches){ stringstream ss; ss<<"3-"<<min(2,lost); lost-=min(2,lost); ans.pb(ss.str()); } rep(i,lostMatches){ stringstream ss; ss<<min(2,won)<<"-3"; won-=min(2,won); ans.pb(ss.str()); } sort(all(ans)); string s; rep(i,ans.size())s+=(i?",":"")+ans[i]; return s; } };