CodeChef December CookOff "The Morning Commute" COMMUTE

問題:http://www.codechef.com/COOK05/problems/COMMUTE/

参加形式:本番

初参加.とりあえず出てみた.

電車の時刻表として始発時間と次の駅までの所要時間と何分ごとに出るかという時間が駅数分与えられる.最初の駅から最後の駅までに到達できる最小の時間を求める.

とりあえず各駅に到達する時間をそれぞれ求めていった.

package main

import (
	"os"
	"strconv"
	"bufio"
	"io/ioutil"
	"fmt"
	//	"container/vector"
	//	"container/heap"
	//	"math"
	//	"sort"
	//	"strings"

)

func run() {
	for t := next(); t > 0; t-- {
		r := 0
		for n := next(); n > 0; n-- {
			a := []int{next(), next(), next()}
			i := 0
			for r > a[0]+i*a[2] {
				i++
			}
			r = a[0] + i*a[2] + a[1]
		}
		println(r)
	}
}

func main() {
	run()
	ioend()
}

// readall input
var (
	buf []byte
	out *bufio.Writer
)

func init() {
	buf, _ = ioutil.ReadAll(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
}

func ioend() {
	out.Flush()
}

func next() (r int) {
	s := 1
	for (buf[0] < '0' || '9' < buf[0]) && buf[0] != '-' {
		buf = buf[1:]
	}
	if buf[0] == '-' {
		s = -1
		buf = buf[1:]
	}
	for '0' <= buf[0] && buf[0] <= '9' {
		r = 10*r + int(buf[0]-'0')
		buf = buf[1:]
	}
	r *= s
	return
}

func nextStr() string {
	return string(nextBytes())
}

func nextBytes() (b []byte) {
	for buf[0] == '\n' || buf[0] == ' ' {
		buf = buf[1:]
	}
	p := 0
	for buf[p] != '\n' && buf[p] != ' ' {
		p++
	}
	b = buf[0:p]
	buf = buf[p:]
	return
}

func nextLine() string {
	for buf[0] == '\n' || buf[0] == ' ' {
		buf = buf[1:]
	}
	p := 0
	for buf[p] != '\n' {
		p++
	}
	b := buf[0:p]
	buf = buf[p:]
	return string(b)
}

func print(os ...interface{}) {
	for _, o := range os {
		switch i := o.(type) {
		case string:
			out.WriteString(o.(string))
		case int:
			out.WriteString(strconv.Itoa(o.(int)))
		case int64:
			out.WriteString(strconv.Itoa64(o.(int64)))
		case byte:
			out.WriteByte(o.(byte))
		default:
			out.WriteString(fmt.Sprint(o)) // may be too slow
		}
	}
}

func println(os ...interface{}) {
	for _, o := range os {
		print(o)
	}
	//	print(os...) not work in 7-14-build
	print("\n")
}

func printlnNow(o interface{}) {
	fmt.Println(o)
}

// readall input end