func toBitSet(number uint64) bitset.BitSet {
	lsbString := strconv.FormatUint(number, 2)

	var b bitset.BitSet
	for i, character := range lsbString {
		if character == '1' {
			b.Set(uint(len(lsbString)-1) - uint(i))
		}
	}
	return b
}
Ejemplo n.º 2
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))
}