Exemplo n.º 1
0
func solve(pr *codejam.Problem, d *data) {
	var unitsDigit, tensDigit, hundredsDigit int

	if d.num == 0 {
		unitsDigit, tensDigit, hundredsDigit = 1, 0, 0
	} else if d.num == 1 {
		unitsDigit, tensDigit, hundredsDigit = 5, 0, 0
	} else if d.num == 2 {
		unitsDigit, tensDigit, hundredsDigit = 7, 2, 0
	} else {
		v := (d.num - hundredsOffset) % hundredsPeriod
		u := hundredsLookup[v+hundredsOffset] - 1

		unitsDigit = mod10(u)

		if u >= 10 {
			u = (u - unitsDigit) / 10

			tensDigit = mod10(u)

			if u >= 10 {
				u = (u - tensDigit) / 10

				hundredsDigit = mod10(u)
			}
		}
	}

	pr.Write(fmt.Sprintf("Case #%d: %d%d%d\n", d.testIndex, hundredsDigit, tensDigit, unitsDigit))
}
Exemplo n.º 2
0
func solve(pr *codejam.Problem, d *data) {
	n := len(d.iPos)
	nf := float64(n)

	var sums vector

	for i := 0; i < n; i++ {
		for j := 0; j < 6; j++ {
			sums[j] += d.iPos[i][j] / nf
		}
	}

	alphaSq := sums[0]*sums[0] + sums[1]*sums[1] + sums[2]*sums[2]
	betaSq := sums[3]*sums[3] + sums[4]*sums[4] + sums[5]*sums[5]
	alphaBeta := sums[0]*sums[3] + sums[1]*sums[4] + sums[2]*sums[5]

	tmin := -alphaBeta / betaSq

	var dmin float64

	if tmin < 0.0 || betaSq < 0.00000001 {
		tmin = 0.0
		dmin = math.Sqrt(alphaSq)
	} else {
		x := sums[0] + sums[3]*tmin
		y := sums[1] + sums[4]*tmin
		z := sums[2] + sums[5]*tmin

		dmin = math.Sqrt(x*x + y*y + z*z)
	}

	pr.Write(fmt.Sprintf("Case #%d: %.8f %.8f\n", d.testIndex, dmin, tmin))
}
Exemplo n.º 3
0
func solve(pr *codejam.Problem, testCaseIndex int, patternStr string, root *trie, wordSize int) {
	pattern := string2Pattern(patternStr, wordSize)

	nw := root.count(pattern)

	pr.Write(fmt.Sprintf("Case #%d: %d\n", testCaseIndex, nw))
}
Exemplo n.º 4
0
func solve(pr *codejam.Problem, vd *vectorData) {
	sort.Ints(vd.xs)
	sort.Ints(vd.ys)

	minSum := vd.scalar()

	pr.Write(fmt.Sprintf("Case #%d: %d\n", vd.testIndex, minSum))
}
Exemplo n.º 5
0
func solve(pr *codejam.Problem, d *data) {
	memo := make(map[[4]int]int)

	v := [4]int{0, d.nc + 1, 0, len(d.released)}

	ng := coins(d.released, v, memo)

	pr.Write(fmt.Sprintf("Case #%d: %d\n", d.testIndex, ng))
}
Exemplo n.º 6
0
func solve(pr *codejam.Problem, d *data) {
	root := buildTree(d)

	if symmetric(d, root, 2) {
		pr.Write(fmt.Sprintf("Case #%d: SYMMETRIC\n", d.testIndex))
	} else {
		pr.Write(fmt.Sprintf("Case #%d: NOT SYMMETRIC\n", d.testIndex))
	}
}
Exemplo n.º 7
0
func parse(pr *codejam.Problem, testCaseIndex int) *data {
	d := &data{
		testIndex: testCaseIndex,
	}

	d.val = pr.ReadString()

	return d
}
Exemplo n.º 8
0
func parse(pr *codejam.Problem, testCaseIndex int) *data {
	n := pr.ReadInt()

	d := &data{
		num:       n,
		testIndex: testCaseIndex,
	}

	return d
}
Exemplo n.º 9
0
func solve(pr *codejam.Problem, d *data) {
	var lookup [36]int

	for i := 0; i < 36; i++ {
		lookup[i] = -1
	}

	n := len(d.val)
	if n == 1 {
		pr.Write(fmt.Sprintf("Case #%d: 1\n", d.testIndex))
		return
	}

	countUniq := 0

	for i := 0; i < n; i++ {
		c := d.val[i]
		j := index(c)
		if lookup[j] == -1 {
			if countUniq == 0 {
				lookup[j] = 1
			} else if countUniq == 1 {
				lookup[j] = 0
			} else {
				lookup[j] = countUniq
			}
			countUniq++
		}
	}

	p := 1
	r := 0

	var base int

	if countUniq == 1 {
		base = 2
	} else {
		base = countUniq
	}

	for i := n - 1; i >= 0; i-- {
		c := d.val[i]
		j := index(c)
		if lookup[j] == -1 {
			panic("digit not set")
		}
		r += p * lookup[j]
		p *= base
	}

	pr.Write(fmt.Sprintf("Case #%d: %d\n", d.testIndex, r))
}
Exemplo n.º 10
0
func parse(pr *codejam.Problem, testCaseIndex int) *data {
	d := &data{
		testIndex: testCaseIndex,
	}

	n := uint(pr.ReadInt())
	m := 1 << n

	d.xs = make([]int, m)
	d.xs = pr.ReadInts(d.xs)
	return d
}
Exemplo n.º 11
0
func solve(pr *codejam.Problem, d *data) {
	n := len(d.pairs)

	ais := make([]int, n)
	bis := make([]int, n)

	for i := 0; i < n; i++ {
		ais[i] = i
		bis[i] = i
	}

	psa := &pairSorter{
		indices: ais,
		pairs:   d.pairs,
		column:  0,
	}

	psb := &pairSorter{
		indices: bis,
		pairs:   d.pairs,
		column:  1,
	}

	sort.Sort(psa)
	sort.Sort(psb)

	var nw int

	for i := 0; i < n-1; i++ {
		v := d.pairs[ais[i]][1]

		k := sort.Search(n, func(j int) bool {
			return d.pairs[bis[j]][1] >= v
		})

		// count common indices from ais[i+1:] and bis[:k]
		var ba bitset.BitSet
		var bb bitset.BitSet

		for _, c := range ais[i+1:] {
			ba.Set(uint(c))
		}

		for _, c := range bis[:k] {
			bb.Set(uint(c))
		}

		nw += int(ba.IntersectionCardinality(&bb))
	}

	pr.Write(fmt.Sprintf("Case #%d: %d\n", d.testIndex, nw))
}
Exemplo n.º 12
0
func solve(pr *codejam.Problem, d *data) {
	tscsc, ok := powerSort(2, d.xs)

	if !ok {
		pr.Write(fmt.Sprintf("Case #%d: 0\n", d.testIndex))
	} else {
		ns := 0
		for _, sc := range tscsc {
			ns += swapWays(sc)
		}
		pr.Write(fmt.Sprintf("Case #%d: %d\n", d.testIndex, ns))
	}
}
Exemplo n.º 13
0
func parse(pr *codejam.Problem, testCaseIndex int) *vectorData {
	n := pr.ReadInt()

	vd := &vectorData{
		xs:        make([]int, n),
		ys:        make([]int, n),
		testIndex: testCaseIndex,
	}

	pr.ReadInts(vd.xs)
	pr.ReadInts(vd.ys)

	return vd
}
Exemplo n.º 14
0
func parse(pr *codejam.Problem, testCaseIndex int) *data {
	d := &data{
		testIndex: testCaseIndex,
	}

	n := pr.ReadInt()
	d.iPos = make([]vector, n)

	for i := 0; i < n; i++ {
		pr.ReadFloats(d.iPos[i][:])
	}

	return d
}
Exemplo n.º 15
0
func parse(pr *codejam.Problem, testCaseIndex int) *data {
	d := &data{
		testIndex: testCaseIndex,
	}

	pq := pr.ReadInts(nil)

	d.nc = pq[0]

	d.released = make([]int, pq[1])
	pr.ReadInts(d.released)

	return d
}
Exemplo n.º 16
0
func parse(pr *codejam.Problem, testCaseIndex int) *data {
	d := new(data)
	d.testIndex = testCaseIndex

	n := pr.ReadInt()
	d.pairs = make([]pair, n)

	for i := 0; i < n; i++ {
		xs := pr.ReadInts(nil)
		d.pairs[i][0] = xs[0]
		d.pairs[i][1] = xs[1]
	}
	return d
}
Exemplo n.º 17
0
func solve(pr *codejam.Problem, d *data) {
	var nw int

	root := &node{}

	for _, path := range d.es {
		root.declarePath(path)
	}

	for _, path := range d.ns {
		nw += root.declarePath(path)
	}

	pr.Write(fmt.Sprintf("Case #%d: %d\n", d.testIndex, nw))
}
Exemplo n.º 18
0
func solve(pr *codejam.Problem, d *data) {
	b := make(batch, d.flavors)

	for _, c := range d.customers {
		if len(c.malted) == 1 && len(c.unmalted) == 0 {
			v := c.malted[0]
			b[v] = 1
			d.malted(b, v)
		}
	}

	found := d.search(b)

	if found {
		pr.Write(fmt.Sprintf("Case #%d: %s\n", d.testIndex, b))
	} else {
		pr.Write(fmt.Sprintf("Case #%d: IMPOSSIBLE\n", d.testIndex))
	}
}
Exemplo n.º 19
0
func solve(pr *codejam.Problem, d *data) {
	// bigger than any number of swaps
	minZero := 4*len(d.m) + 1

	ns := minZero
	for i := 0; i < 2; i++ {
		for j := 0; j < 2; j++ {
			n, ok := solveF(d.m, byte(i), byte(j))
			if ok && n < ns {
				ns = n
			}
		}
	}

	if ns == minZero {
		pr.Write(fmt.Sprintf("Case #%d: IMPOSSIBLE\n", d.testIndex))
	} else {
		pr.Write(fmt.Sprintf("Case #%d: %d\n", d.testIndex, ns))
	}
}
Exemplo n.º 20
0
func parse(pr *codejam.Problem, testCaseIndex int) *data {
	ps := pr.ReadInts(nil)

	d := &data{
		testIndex: testCaseIndex,
	}

	d.points = make([]float64, len(ps)-1)

	for i := 0; i < len(d.points); i++ {
		v := float64(ps[i+1])
		d.points[i] = v
		d.sum += v
	}

	d.n = float64(len(d.points))

	d.denom = d.n * d.sum
	return d
}
Exemplo n.º 21
0
func solve(pr *codejam.Problem, d *data) {
	alphas := make([]float64, len(d.points))

	for k, _ := range d.points {
		alphas[k] = d.search(k)
	}

	var buf bytes.Buffer

	first := true
	for _, v := range alphas {
		if first {
			first = false
		} else {
			buf.WriteString(" ")
		}
		buf.WriteString(strconv.FormatFloat(v*100.0, 'f', 9, 64))
	}

	pr.Write(fmt.Sprintf("Case #%d: %s\n", d.testIndex, buf.String()))
}
Exemplo n.º 22
0
func parse(pr *codejam.Problem, testCaseIndex int) *data {
	n := pr.ReadInt()
	m := pr.ReadInt()

	d := &data{
		flavors:             n,
		customers:           make([]*customer, m),
		customersByUnmalted: make([][]*customer, n),
		customersByMalted:   make([][]*customer, n),
		testIndex:           testCaseIndex,
	}

	for i := 0; i < m; i++ {
		nums := pr.ReadInts(nil)

		c := &customer{
			malted:   make([]int, 0, nums[0]),
			unmalted: make([]int, 0, nums[0]),
		}

		for j := 0; j < nums[0]; j++ {
			mv := nums[2*j+2] == 1
			fv := nums[2*j+1] - 1

			if mv {
				c.malted = append(c.malted, fv)
				d.customersByMalted[fv] = append(d.customersByMalted[fv], c)
			} else {
				c.unmalted = append(c.unmalted, fv)
				c.satisfied = true
				d.customersByUnmalted[fv] = append(d.customersByUnmalted[fv], c)
			}
		}

		d.customers[i] = c
	}

	return d
}
Exemplo n.º 23
0
func parse(pr *codejam.Problem, testCaseIndex int) *data {
	d := &data{
		testIndex: testCaseIndex,
	}

	n := pr.ReadInt()

	d.m = make([][]byte, 2*n)

	for i := 0; i < 2*n; i++ {
		d.m[i] = make([]byte, 2*n)

		str := pr.ReadString()

		for j := 0; j < 2*n; j++ {
			if str[j] == '1' {
				d.m[i][j] = 1
			} else {
				d.m[i][j] = 0
			}
		}
	}
	return d
}
Exemplo n.º 24
0
func parse(pr *codejam.Problem, testCaseIndex int) *data {
	d := &data{
		testIndex: testCaseIndex,
	}

	nm := pr.ReadInts(nil)
	n := nm[0]
	m := nm[1]

	d.es = make([]string, n)
	d.ns = make([]string, m)

	for i := 0; i < n; i++ {
		d.es[i] = pr.ReadString()
	}

	for i := 0; i < m; i++ {
		d.ns[i] = pr.ReadString()
	}

	return d
}
Exemplo n.º 25
0
func parse(pr *codejam.Problem, testCaseIndex int) *data {
	d := &data{
		testIndex: testCaseIndex,
	}

	n := pr.ReadInt()
	d.vertices = make([]string, n)
	d.edges = make([][]int, n)

	for i := 0; i < n; i++ {
		d.vertices[i] = pr.ReadString()
	}

	for i := 0; i < n-1; i++ {
		edge := pr.ReadInts(nil)
		a, b := edge[0]-1, edge[1]-1

		d.edges[a] = append(d.edges[a], b)
		d.edges[b] = append(d.edges[b], a)
	}

	return d
}