GCJ2011 Qualification Round A Bot Trust
GCJ2011に参加できなかったので,とりあえず今年の問題や過去問を解いてプログラミングの練習でもしてみようという趣旨です.とりあえず今年のQualification Roundから.
http://code.google.com/codejam/contest/dashboard?c=975485
問題
- BlueとOrangeのロボットが並行する2本の廊下にいる.
- 各廊下の長さは100であり,それぞれ1mごとに{1, 2, ..., 100}のボタンがある.
- ロボットはそれぞれボタン1の場所から始める
- ロボットは地点を移動するのに1秒,ボタンを1回押すのに1秒かかる
- ロボットはあるボタンを押すためのリストを渡され,その順序でボタンを押す必要がある
- 全部で何秒かかるか?
例
- O 2, B 1, B 2, O 4のようなリストの時,以下のような動きをする.
Time | Orange | Blue
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
プログラム
import java.util.*; import java.io.*; public class Qualifer_01_2011 { public void solve() throws Exception { Scanner sc = new Scanner(new FileReader("./input/A-large-practice.in")); PrintWriter pw = new PrintWriter(new FileWriter("./output/Q01_2011output.txt")); int T = sc.nextInt();//Case数 for (int caseNum = 0; caseNum < T; caseNum++) { //outputの書式 pw.print("Case #" + (caseNum + 1) + ": "); int curO = 1, spareStepsO = 0, curB = 1, spareStepsB = 0; //各ケースのボタンを押す回数(リストの長さ) int N = sc.nextInt(); int res = 0; for (int i=0; i < N; i++) { String who = sc.next(); if (who.equals("O")) { int pos = sc.nextInt(); int Osteps = Math.abs(curO - pos) + 1; Osteps -= Math.min(spareStepsO, Osteps - 1); res += Osteps; spareStepsB += Osteps; //相手のスペアステップを増やす spareStepsO = 0; //自分のスペアステップをリセット curO = pos; } else { int pos = sc.nextInt(); int Bsteps = Math.abs(curB - pos) + 1; Bsteps -= Math.min(spareStepsB, Bsteps - 1); res += Bsteps; spareStepsO += Bsteps; spareStepsB = 0; curB = pos; } } pw.println(res); } pw.flush(); pw.close(); sc.close(); } public static void main(String[] args) throws Exception { new Qualifer_01_2011().solve(); } }