-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpoles.go
More file actions
77 lines (67 loc) · 1.39 KB
/
poles.go
File metadata and controls
77 lines (67 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"sort"
)
type pole struct {
altitude int
weight int
}
type Cost struct {
Key int
Value int
}
type CostList []Cost
func (p CostList) Len() int {
return len(p)
}
func (p CostList) Less(i, j int) bool {
return p[i].Value < p[j].Value
}
func (p CostList) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func main() {
totalCost := 0
var poles int
if m, err := fmt.Scan(&poles); m != 1 || (m < 1 || m > 5000) {
panic(err)
}
var stacks int
if m, err := fmt.Scan(&stacks); m != 1 || (m < 1 || m > poles) {
panic(err)
}
poleMap := make(map[int]pole, poles)
costMap := make(CostList, poles)
for i := 0; i < poles; i++ {
p1 := pole{}
if m, err := fmt.Scan(&p1.altitude); m != 1 || (m < 1 || m > 1000000) {
panic(err)
}
if m, err := fmt.Scan(&p1.weight); m != 1 || (m < 1 || m > 1000000) {
panic(err)
}
poleMap[i] = p1
if (i != 0) {
cost := (poleMap[i].altitude - poleMap[i - 1].altitude) * (poleMap[i].weight)
costMap[i] = Cost{i, cost}
}
}
if (stacks == 1) {
for _, v := range costMap {
totalCost += v.Key * v.Value
}
fmt.Println(totalCost)
} else {
fmt.Println(findTheBreaks(stacks, costMap))
}
}
func findTheBreaks(stacks int, costList CostList) []int {
sort.Sort(sort.Reverse(costList))
fmt.Println(costList)
breaks := make([]int, stacks-1)
for i := 0; i < stacks-1; i++ {
breaks[i] = costList[i].Key
}
return breaks
}