package main import ( "bufio" "fmt" "os" "strings" "strconv" ) func main() { sc := bufio.NewScanner(os.Stdin) for sc.Scan() { strs := strings.Split(sc.Text(), ",") arr := make([]int, 0, len(strs)) var number int for _, str := range strs { number, _ = strconv.Atoi(str) arr = append(arr, number) } fmt.Println(dispatchCandy(arr)) } } func dispatchCandy(arr []int) int { dp := make([]int, len(arr)) // 至少分得一个 for i := range dp { dp[i] = 1 } // 分两个方向考虑,不然顾此失彼 // 从前往后,找分数比左邻居高 for i := 1; i < len(arr); i++ { if arr[i] > arr[i-1] && dp[i] <= dp[i-1] { dp[i] = dp[i-1]+1 } } // 从后往前,找分数比右邻居高 for j := len(arr)-2; j >= 0; j-- { if arr[j] > arr[j+1] && dp[j] <= dp[j+1] { dp[j] = dp[j+1]+1 } } sum := 0 for _, v := range dp { sum += v } return sum }