·  阅读 2295

hello大家好呀，我是小楼。

# 10分钟写出入门题，但...

``````package main

import (
"fmt"
)

func main() {
var n int
var name string
var x float32
var max float32
scores := make(map[string]float32, n)

fmt.Scan(&n)
for i := 0; i < n; i++ {
fmt.Scan(&name, &x)

if x > max || i == 0 {
fmt.Println("perfect")
max = x
scores[name] = x
} else {
if s, ok := scores[name]; ok {
if x > s {
fmt.Println("great")
scores[name] = x
} else {
}
} else {
fmt.Println("great")
scores[name] = x
}
}
}
}

# 优化map性能

• 时间限制：C/C++ 1秒，其他语言2秒
• 空间限制：C/C++ 262144K，其他语言524288K
• 64bit IO Format: %lld

• name保证长度不超过6，仅由小写英文字母组成，每个名字代表唯一一个同学
• x为1位小数，0≤x≤300

name最长为6，且为小写字母，这点给了我一点启发，能不能让查询map变成O(1)复杂度？

26 * 27^5 + 26 * 27^4 + 26 * 27^3 + 26 * 27^2 + 26 * 27^1 + 26 * 27^0

387420488

``````package main

import (
"fmt"
)

func main() {
var n int
var name string
var x int32

fmt.Scan(&n)

var scores [387420488]int32
var exist [387420488]int32

var max int32
for i := 0; i < n; i++ {
fmt.Scan(&name, &x)

idx := mapIndex(name)

if x > max || i == 0 {
fmt.Printf("perfect\n")
max = x
scores[idx] = x
exist[idx] = 1
} else {
if exist[idx] == 0 || x > scores[idx] {
fmt.Printf("great\n")
scores[idx] = x
exist[idx] = 1
} else {
}
}
}
}

var index27 = [6]int32{1, 27, 27 * 27, 27 * 27 * 27, 27 * 27 * 27 * 27}

func mapIndex(x string) int32 {
var index int32
for i := len(x) - 1; i >= 0; i-- {
index = index + int32(x[i]-96)*index27[len(x)-1-i]
}
return index
}

# 优化内存占用

``````var scores [387420488]int32
var exist [387420488]int32

exist数组可以用boolean类型，分数最大值0<=0<=300，int16足矣

int81字节-128 ~ 127
int162字节-32768 ~ 32767
int324字节-2147483648 ~ 2147483647

# 赛后思考

``````package main

import (
"fmt"
)

type treeNode struct {
max  float32
next [26]*treeNode
}

func main() {
var n int
var name string
var x float32
fmt.Scan(&n)

var max float32
tree := new(treeNode)
for i := 0; i < n; i++ {
fmt.Scan(&name, &x)

if x > max || i == 0 {
fmt.Println("perfect")
max = x
insert(tree, name, x)
} else {
if tmp := searchAndStoreMax(tree, name, x); tmp != -1 {
if x > tmp {
fmt.Println("great")
insert(tree, name, x)
} else {
}
} else {
fmt.Println("great")
insert(tree, name, x)
}
}
}
}

func insert(node *treeNode, name string, x float32) {
for i := 0; i < len(name); i++ {
idx := int32(name[i] - 'a')
if node.next[idx] == nil {
node.next[idx] = new(treeNode)
}
node = node.next[idx]
}
node.max = x
}

func searchAndStoreMax(node *treeNode, name string, x float32) float32 {
for i := 0; i < len(name); i++ {
idx := int32(name[i] - 'a')
if node.next[idx] == nil {
return -1
}
node = node.next[idx]
}
if x > node.max {
tmp := node.max
node.max = x
return tmp
}
return node.max
}

# 终于发现问题

``````package main

import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)

func main() {
var n int
var name string
var x float64
input := bufio.NewScanner(os.Stdin)
if input.Scan() {
n, _ = strconv.Atoi(input.Text())
}

scores := make(map[string]float64, n)
var max float64
for i := 0; i < n; i++ {
if input.Scan() {
arr := strings.Split(input.Text(), " ")
name = arr[0]
x, _ = strconv.ParseFloat(arr[1], 32)
}

...
}
}

map版315ms10096KB
27进制版--