区间选点-golang
blablabla
区间选点-golang
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
)
var n int
type node struct {
l int
r int
}
var count int
var scan *bufio.Scanner
func main() {
scan = bufio.NewScanner(os.Stdin)
fmt.Scanf("%d", &n)
arr := make([]node, n)
for i := 0; i < n; i++ {
arr[i].l, arr[i].r = scanLine()
}
sort.SliceStable(arr, func(i, j int) bool {
return arr[i].r < arr[j].r
})
edge := int(-2e9)
for i := 0; i < n; i++ {
if arr[i].l > edge {
count++
edge = arr[i].r
}
}
fmt.Println(count)
}
func scanLine() (int, int) {
scan.Scan()
var num int
for {
str := strings.Split(scan.Text(), " ")
if len(str) != 2 {
num++
fmt.Println(num)
continue
}
left, _ := strconv.Atoi(str[0])
right, _ := strconv.Atoi(str[1])
return left, right
}
}