問題:https://code.google.com/codejam/contest/dashboard?c=975485#s=p0
Google Code Jam nise..nabeで参加.
二つのロボットがスイッチを押す命令が順番に与えられる.動く,スイッチを押す,待機する,それぞれの行動に1分かかるとき,与えられた命令を遂行するのに必要な時間を求める.
単純にシミュレーション.
ソース
package main import ( "os" "bufio" "container/list" "fmt" "io" "io/ioutil" "strconv" ) func run() { for t, T := 1, s.next(); t <= T; t++ { n := s.next() blue, orange := list.New(), list.New() for i := 0; i < n; i++ { r, in := s.nextStr(), s.next() switch { case r == "B": blue.PushBack([]int{i, in}) case r == "O": orange.PushBack([]int{i, in}) } } i, r := 0, 0 bp, op := 1, 1 b, o := blue.Front(), orange.Front() f := func(i int, p *int, in []int) (b bool) { if len(in) == 0 { return false } switch v := in[1] - *p; { case v < 0: *p-- b = false case v > 0: *p++ b = false case v == 0 && in[0] == i: b = true } return } for ; b != nil || o != nil; r++ { bb,ob := false,false if b != nil { if bb = f(i,&bp,b.Value.([]int));bb { b = b.Next() } } if o != nil { if ob = f(i,&op,o.Value.([]int));ob { o = o.Next() } } if bb || ob { i++ } } s.println("Case #", t, ": ", r) } } func main() { s = NewInOut(os.Stdin, os.Stdout) run() s.Flush() } var ( s *InOut ) // io start type InOut struct { in []byte *bufio.Writer } func NewInOut(r io.Reader, w io.Writer) *InOut { in, _ := ioutil.ReadAll(r) return &InOut{in, bufio.NewWriter(w)} } func (s *InOut) next() (r int) { buf := s.in p := 1 for (buf[0] < '0' || '9' < buf[0]) && buf[0] != '-' { buf = buf[1:] } if buf[0] == '-' { p = -1 buf = buf[1:] } for '0' <= buf[0] && buf[0] <= '9' { r = 10*r + int(buf[0]-'0') buf = buf[1:] } r *= p s.in = buf return } func (s *InOut) nextStr() (r string) { buf := s.in for buf[0] == '\n' || buf[0] == ' ' { buf = buf[1:] } p := 0 for buf[p] != '\n' && buf[p] != ' ' { p++ } r = string(buf[0:p]) s.in = buf[p:] return } func (s *InOut) print(os ...interface{}) { for _, o := range os { switch i := o.(type) { case byte: s.WriteByte(o.(byte)) case string: s.WriteString(o.(string)) case int: s.WriteString(strconv.Itoa(o.(int))) case int64: s.WriteString(strconv.Itoa64(o.(int64))) default : s.WriteString(fmt.Sprint(o)) } } } func (s *InOut) println(os ...interface{}) { for _,o := range os{ s.print(o) } s.print("\n") } func (s *InOut) printlnNow(os ...interface{}) { for _, o := range os { fmt.Println(o) } } // io end