반응형
https://www.acmicpc.net/problem/10814
sort package에 stable sort가 있긴한데 그냥 안썼다.
package main
import (
"bufio"
"fmt"
"os"
"sort"
)
var writer = bufio.NewWriter(os.Stdout)
var reader = bufio.NewReader(os.Stdin)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
type Person struct {
age int
order int
name string
}
type SortBy []Person
func (a SortBy) Len() int { return len(a) }
func (a SortBy) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SortBy) Less(i, j int) bool {
if a[i].age == a[j].age {
return a[i].order < a[j].order
}
return a[i].age < a[j].age
}
func main() {
defer writer.Flush()
var n int
scanf("%d\n", &n)
arr := make(SortBy, n)
for i := 0; i < n; i++ {
scanf("%d %s\n", &arr[i].age, &arr[i].name)
arr[i].order = i
}
sort.Sort(arr)
for _, i := range arr {
printf("%d %s\n", i.age, i.name)
}
}
'Study > algorithms' 카테고리의 다른 글
[백준] 15651 N과 M (3) (0) | 2020.01.27 |
---|---|
[백준] 15649 N과 M (1) (0) | 2020.01.26 |
[백준] 11651 좌표 정렬하기 2 (0) | 2020.01.25 |
[백준] 11650 좌표 정렬하기 (0) | 2020.01.25 |
[백준] 2108 통계학 (0) | 2020.01.25 |