ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

12789๋ฒˆ: ๋„ํ‚ค๋„ํ‚ค ๊ฐ„์‹๋“œ๋ฆฌ๋ฏธ

์ธํ•˜๋Œ€ํ•™๊ต ํ•™์ƒํšŒ์—์„œ๋Š” ์ค‘๊ฐ„, ๊ธฐ๋ง๊ณ ์‚ฌ ๋•Œ๋งˆ๋‹ค ์‹œํ—˜ ๊ณต๋ถ€์— ์ง€์นœ ํ•™์šฐ๋“ค์„ ์œ„ํ•ด ๊ฐ„์‹์„ ๋‚˜๋ˆ ์ฃผ๋Š” ๊ฐ„์‹ ๋“œ๋ฆฌ๋ฏธ ํ–‰์‚ฌ๋ฅผ ์‹ค์‹œํ•œ๋‹ค. ์Šนํ™˜์ด๋Š” ์‹œํ—˜ ๊ธฐ๊ฐ„์ด ๋  ๋•Œ๋งˆ๋‹ค ๊ฐ„์‹์„ ๋ฐ›์„ ์ƒ๊ฐ์— ๋‘๊ทผ๋‘

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/12789
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanln(reader, &n)
	var students = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d ", &students[i])
	}
	fmt.Fprintln(writer, getResult(students))
}

func getResult(students []int) string {
	var stack []int
	var line []int
	for len(students) > 0 {
		if len(stack) == 0 {
			stack = append(stack, students[0])
			students = students[1:]
		} else {
			if stack[len(stack)-1] > students[0] {
				stack = append(stack, students[0])
				students = students[1:]
			} else {
				line = append(line, stack[len(stack)-1])
				students = students[1:]
			}
		}
	}
	for len(stack) > 0 {
		line = append(line, stack[len(stack)-1])
		stack = stack[:len(stack)-1]
	}
	if sort.SliceIsSorted(line, func(i, j int) bool {
		return line[i] < line[j]
	}) {
		return "Nice"
	}
	return "Sad"
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/12789
import sys

def get_result(students):
    stack = []
    line = []
    while len(students) > 0:
        if len(stack) == 0:
            stack.append(students.pop(0))
        else:
            if stack[-1] > students[0]:
                stack.append(students.pop(0))
            else:
                line.append(stack[-1])
                students.pop(0)
    while len(stack) > 0:
        line.append(stack[-1])
        stack.pop(-1)
    if line == sorted(line):
        return "Nice"
    return "Sad"

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    students = list(map(int, sys.stdin.readline().rstrip().split()))
    print(get_result(students))
728x90
๋Œ“๊ธ€