Skip to content

Instantly share code, notes, and snippets.

@robert-king
Created October 18, 2014 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robert-king/4fd0818a4ef6c601541a to your computer and use it in GitHub Desktop.
Save robert-king/4fd0818a4ef6c601541a to your computer and use it in GitHub Desktop.
Codechef TSORT (Turbo Sort)
package main
import (
"os"
)
//google.com/+robertking
//fastest go solutions:
//http://www.codechef.com/status/TSORT?sort_by=Time&sorting_order=asc&language=114&status=15&handle=&Submit=GO
//(It's 3.8x slower than C & C++. Slightly faster than Java.)
func main() {
data := make([]byte, 1000001 * 7)
counts := make([]int, 1000001)
os.Stdin.Read(data)
i := 0
T := 0
//read number of cases
for ;;i++ {
if (data[i] == '\n') {
i++
break;
}
T *= 10;
T += int(data[i] - '0')
}
x := 0
//get counts for each number
for ;;i++ {
if (data[i] == '\n' || data[i] == 0) {
counts[x]++
T--
if (T == 0) {
break
}
x = 0;
} else {
x = (x<<3) + (x<<1) + int(data[i] - '0')
}
}
//write output
bytes := make([]byte, 1000001 * 7)
j := 0
for x := len(counts) - 1; x >= 0; x-- {
c := counts[x]
for ; c > 0; c-- {
x2 := x
for {
bit := x2 % 10
if (x2 < 1029) {
x2 = (x2 * 205) >> 11
} else {
x2 /= 10
}
bytes[j] = byte(bit) + '0'
j++
if x2 == 0 {
break
}
}
bytes[j] = '\n'
j++
}
}
//reverse
upto := j
j -= 2
i = 0
for ; i < j; {
bytes[i], bytes[j] = bytes[j], bytes[i]
i++
j--
}
//write
os.Stdout.Write(bytes[:upto])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment