Skip to content

Commit cc8c937

Browse files
committed
Add Go
1 parent af732a3 commit cc8c937

4 files changed

Lines changed: 94 additions & 1 deletion

File tree

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ language: crystal
33
before_install:
44
- sudo apt-get -qq update
55
- sudo apt-get install -y default-jdk haskell-platform mono-mcs
6+
- gimme 1.9.2
7+
- go version
68

79
install: make clean build
810

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ build:
1616
javac -d $(BIN_DIR) Main.java
1717
ghc -O2 main.hs -outputdir=$(BUILD_DIR) -o $(BIN_DIR)/haskell
1818
mcs main.cs -out:bin/csharp -optimize+
19+
go build -o $(BIN_DIR)/go main.go
1920

2021
test:
2122
mkdir $(TMP_DIR)
@@ -35,4 +36,6 @@ test:
3536
@cmp $(TMP_DIR)/crystal.out $(TMP_DIR)/python3.out || (echo "Failed: Python3" && exit 1)
3637
bin/csharp < tmp/test_data.in > tmp/csharp.out
3738
@cmp $(TMP_DIR)/crystal.out $(TMP_DIR)/csharp.out || (echo "Failed: C#" && exit 1)
39+
$(BIN_DIR)/go < $(TMP_DIR)/test_data.in > $(TMP_DIR)/go.out
40+
@cmp $(TMP_DIR)/crystal.out $(TMP_DIR)/go.out || (echo "Failed: Go" && exit 1)
3841
@echo "Passed"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Solve 2D Rank Finding Problem in Several Languages
1414
- Haskell
1515
- Python 3
1616
- C#
17+
- Go
1718

1819
## Problem
1920

@@ -80,5 +81,5 @@ Your output should consist of a single line for each test case containing n numb
8081
- [c910335](https://github.com/c910335) Tatsujin Chin - creator, maintainer
8182
- [scps940707](https://github.com/scps940707) YyWang - Java
8283
- [sifmelcara](https://github.com/sifmelcara) mingchuan - Haskell
83-
- [chuanchan1116](https://github.com/chuanchan1116) William Tsai - Python 3
84+
- [chuanchan1116](https://github.com/chuanchan1116) William Tsai - Python 3, Go
8485
- [sharknevercries](https://github.com/sharknevercries) ZhengYuan Lee - C#

main.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import(
4+
"fmt"
5+
"bufio"
6+
"os"
7+
"strconv"
8+
"sort"
9+
)
10+
11+
12+
type Point struct {
13+
i, x, y int
14+
}
15+
16+
var rank []int
17+
18+
func findRank(p []Point) []Point{
19+
if len(p) <= 1 {
20+
return p
21+
}
22+
var ret []Point
23+
i, li, ri := 0, 0, 0
24+
l := findRank(p[:int(len(p)/2)])
25+
r := findRank(p[int(len(p)/2):])
26+
ret = make([]Point, len(p))
27+
for {
28+
if l[li].y <= r[ri].y {
29+
ret[i] = l[li]
30+
li++
31+
if li >= len(l) {
32+
for _, v := range r[ri:] {
33+
rank[v.i] += li
34+
ret[i+1] = v
35+
i++
36+
}
37+
break
38+
}
39+
} else {
40+
ret[i] = r[ri]
41+
rank[r[ri].i] += li
42+
ri++
43+
if ri >= len(r) {
44+
copy(ret[i+1:], l[li:])
45+
break
46+
}
47+
}
48+
i++
49+
}
50+
copy(p, ret)
51+
return p
52+
}
53+
54+
func main() {
55+
scanner := bufio.NewScanner(os.Stdin)
56+
scanner.Split(bufio.ScanWords)
57+
out := bufio.NewWriter(os.Stdout)
58+
defer out.Flush()
59+
for scanner.Scan() {
60+
n, _ := strconv.Atoi(scanner.Text())
61+
if n == 0 {
62+
break
63+
}
64+
var point []Point
65+
rank = make([]int, n)
66+
for i := 0; i < n; i++ {
67+
scanner.Scan()
68+
x, _ := strconv.Atoi(scanner.Text())
69+
scanner.Scan()
70+
y, _ := strconv.Atoi(scanner.Text())
71+
point = append(point, Point{i, x, y})
72+
}
73+
sort.Slice(point, func(i, j int) bool {
74+
if point[i].x != point[j].x {
75+
return point[i].x < point[j].x
76+
} else {
77+
return point[i].y < point[j].y
78+
}
79+
})
80+
findRank(point)
81+
fmt.Fprintf(out, "%d",rank[0])
82+
for i := 1; i < len(rank); i++ {
83+
fmt.Fprintf(out, " %d", rank[i])
84+
}
85+
fmt.Fprintf(out, "\n")
86+
}
87+
}

0 commit comments

Comments
 (0)