Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8-wkdghdwns199 #36

Merged
merged 3 commits into from
Feb 26, 2024
Merged

8-wkdghdwns199 #36

merged 3 commits into from
Feb 26, 2024

Conversation

wkdghdwns199
Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 commented Feb 23, 2024

πŸ”— 문제 링크

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

문제
μΈν•˜λŒ€ν•™κ΅ ν•™μƒνšŒμ—μ„œλŠ” 쀑간, 기말고사 λ•Œλ§ˆλ‹€ μ‹œν—˜ 곡뢀에 μ§€μΉœ ν•™μš°λ“€μ„ μœ„ν•΄ 간식을 λ‚˜λˆ μ£ΌλŠ” 간식 λ“œλ¦¬λ―Έ 행사λ₯Ό μ‹€μ‹œν•œλ‹€. μŠΉν™˜μ΄λŠ” μ‹œν—˜ 기간이 될 λ•Œλ§ˆλ‹€ 간식을 받을 생각에 두근두근 μ„€λ ˆμ„œ μ‹œν—˜ 곡뢀에 집쀑을 λͺ» ν•œλ‹€. 이번 μ€‘κ°„κ³ μ‚¬μ—μ„œλ„ μ—­μ‹œ μŠΉν™˜μ΄λŠ” μ„€λ ˆλŠ” κ°€μŠ΄μ„ μ•ˆκ³  간식을 λ°›κΈ° μœ„ν•΄ 미리 κ³΅μ§€λœ μž₯μ†Œμ— μ‹œκ°„ 맞좰 λ„μ°©ν–ˆλ‹€. 그런데 이게 무슨 날벼락인가! κ·Έ κ³³μ—λŠ” 이미 λͺ¨λ“  학생듀이 λͺ¨μ—¬μžˆμ—ˆκ³ , μŠΉν™˜μ΄λŠ” λ§ˆμ§€λ§‰ λ²ˆν˜Έν‘œλ₯Ό λ°›κ²Œ λ˜μ—ˆλ‹€. μ„€μƒκ°€μƒμœΌλ‘œ λͺ‡λͺ‡ 양심에 털이 λ‚œ 학생듀이 μƒˆμΉ˜κΈ°λ₯Ό κ±°λ“­ν•œ 끝에 λŒ€κΈ°μ—΄μ˜ μˆœμ„œλ§ˆμ € 엉망이 되고 λ§μ•˜λ‹€. 간식을 λ‚˜λˆ μ£Όκ³  있던 μΈκ·œλŠ” ν•™μš°λ“€μ˜ ν„°μ Έ λ‚˜μ˜€λŠ” λΆˆλ§Œμ— λ²ˆν˜Έν‘œ μˆœμ„œλ‘œλ§Œ 간식을 쀄 수 μžˆλ‹€κ³  λ§ν–ˆλ‹€.

κ·Έμ œμ•Ό 학생듀이 μˆœμ„œλŒ€λ‘œ 쀄을 μ„œλ €κ³  ν–ˆμ§€λ§Œ 곡간이 λ„ˆλ¬΄ ν˜‘μ†Œν•΄μ„œ λ§ˆμŒλŒ€λ‘œ 이동할 수 μ—†μ—ˆλ‹€. λ‹€ν–‰νžˆλ„ λŒ€κΈ°μ—΄μ˜ μ™Όμͺ½μ—λŠ” 1μ—΄λ‘œ μ„€ 수 μžˆλŠ” 곡간이 μ‘΄μž¬ν•˜μ—¬ 이 곡간을 잘 μ΄μš©ν•˜λ©΄ λͺ¨λ‘κ°€ μˆœμ„œλŒ€λ‘œ 간식을 받을 수 μžˆμ„μ§€λ„ λͺ¨λ₯Έλ‹€. 자칫 간식을 λͺ» λ°›κ²Œ 될지도 λͺ¨λ₯Έλ‹€λŠ” μœ„κΈ°κ°μ„ λŠλ‚€ μŠΉν™˜μ΄λŠ” μžμ‹ μ˜ 컴퓨터 μ•Œκ³ λ¦¬μ¦˜μ  지식을 ν™œμš©ν•΄ κ³Όμ—° λͺ¨λ“  μ‚¬λžŒλ“€μ΄ μˆœμ„œλŒ€λ‘œ 간식을 받을 수 μžˆλŠ”μ§€ ν™•μΈν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“€κΈ°λ‘œ ν–ˆλ‹€. λ§Œμ•½ λΆˆκ°€λŠ₯ ν•˜λ‹€λ©΄ μŠΉν™˜μ΄λŠ” 이번 쀑간고사λ₯Ό 망치게 될 것 이고 κ°€λŠ₯ν•˜λ‹€λ©΄ νž˜μ„ μ–»μ–΄ 쀑간고사λ₯Ό 잘 λ³Ό 수 μžˆμ„μ§€λ„ λͺ¨λ₯Έλ‹€.

μ‚¬λžŒλ“€μ€ ν˜„μž¬ 1μ—΄λ‘œ 쀄을 μ„œμžˆκ³ , 맨 μ•žμ˜ μ‚¬λžŒλ§Œ 이동이 κ°€λŠ₯ν•˜λ‹€. μΈκ·œλŠ” λ²ˆν˜Έν‘œ μˆœμ„œλŒ€λ‘œλ§Œ 톡과할 수 μžˆλŠ” 라인을 λ§Œλ“€μ–΄ λ‘μ—ˆλ‹€. 이 라인과 λŒ€κΈ°μ—΄μ˜ 맨 μ•ž μ‚¬λžŒ μ‚¬μ΄μ—λŠ” ν•œ μ‚¬λžŒμ”© 1열이 λ“€μ–΄κ°ˆ 수 μžˆλŠ” 곡간이 μžˆλ‹€. ν˜„μž¬ λŒ€κΈ°μ—΄μ˜ μ‚¬λžŒλ“€μ€ 이 κ³΅κ°„μœΌλ‘œ 올 수 μžˆμ§€λ§Œ λ°˜λŒ€λŠ” λΆˆκ°€λŠ₯ν•˜λ‹€. μŠΉν™˜μ΄λ₯Ό 도와 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ.

ν˜„μž¬ 간식 λ°°λΆ€ 곡간을 그림으둜 λ‚˜νƒ€λ‚΄λ©΄ λ‹€μŒκ³Ό κ°™λ‹€.

μœ„ μ˜ˆμ œλŠ” λ‹€μŒ κ·Έλ¦Όκ³Ό 같이 μ›€μ§μ˜€μ„ λ•Œ λͺ¨λ‘κ°€ μˆœμ„œλŒ€λ‘œ 간식을 받을 수 μžˆλ‹€..

μž…λ ₯
μž…λ ₯의 첫째 μ€„μ—λŠ” ν˜„μž¬ μŠΉν™˜μ΄μ˜ μ•žμ— μ„œ μžˆλŠ” ν•™μƒλ“€μ˜ 수 N(1 ≀ N ≀ 1,000,μžμ—°μˆ˜)이 주어진닀.

λ‹€μŒ μ€„μ—λŠ” μŠΉν™˜μ΄ μ•žμ— μ„œμžˆλŠ” λͺ¨λ“  ν•™μƒλ“€μ˜ λ²ˆν˜Έν‘œ(1,2,...,N) μˆœμ„œκ°€ μ•žμ—μ„œλΆ€ν„° λ’€ μˆœμ„œλ‘œ 주어진닀.

좜λ ₯
μŠΉν™˜μ΄κ°€ λ¬΄μ‚¬νžˆ 간식을 받을 수 있으면 "Nice"(λ”°μ˜΄ν‘œλŠ” μ œμ™Έ)λ₯Ό 좜λ ₯ν•˜κ³  그렇지 μ•Šλ‹€λ©΄ "Sad"(λ”°μ˜΄ν‘œλŠ” μ œμ™Έ)λ₯Ό 좜λ ₯ν•œλ‹€.

예제 μž…λ ₯ 1
5
5 4 1 3 2

예제 좜λ ₯ 1
Nice

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„ - μ™œ μŠ€νƒμœΌλ‘œλ§Œ μ•ˆ λ˜λŠ”κ±°μ§€..? 문제λ₯Ό 잘λͺ» μ΄ν•΄ν•˜κ³  μžˆλ‚˜?

✨ μˆ˜λ„ μ½”λ“œ

μƒˆμΉ˜κΈ° ν•œ μΉœκ΅¬λ“€μ€ μŠ€νƒμ— λ¨Όμ € λ°€μ–΄λ„£κ³  1λ²ˆλΆ€ν„° 간식을 λ°›κ²Œ ν•œ λ‹€μŒ, μŠ€νƒμ— μžˆλŠ” μΉœκ΅¬λ“€μ„ λΉΌλ‚΄μ£Όλ €κ³  ν–ˆλŠ”λ° μ‹œκ°„μ΄ˆκ³Όκ°€ λ–΄μŠ΅λ‹ˆλ‹€!

μ‹œκ°„μ΄ˆκ³Όκ°€ λ‚˜λŠ” μ½”λ“œ [μ™œ λ‚¬λŠ”μ§€ μ•„μ‹œκ² μœΌλ©΄ λ°”λ‘œ λŒ“κΈ€ λ‹¬μ•„μ£Όμ„Έμš”.. γ…œγ…œ]

N = int(input())
order_list = list(map(int, input().split()))
order = 1
stack = []

for student in order_list :
    if student > order :
        stack.append(student)
    elif student == order :
        order+=1

while len(stack) !=0 :
    if len(stack) == 1 or stack[len(stack)-2] > stack[len(stack)-1] : 
        stack.pop()
    else : break

print('Nice') if len(stack)==0 else print('Sad')

ν•΄κ²° : μˆœμ„œλ₯Ό μ§€μΉ­ν•˜λŠ” λ³€μˆ˜ order λ₯Ό μ΅œμ‹ ν™” μ‹œν‚€μ§€ μ•Šμ•„μ„œ λ¬΄ν•œλ£¨ν”„μ— 빠진 λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€!

N = int(input())
order_list = list(map(int, input().split()))
order = 1
stack = []

while order_list :
    if order_list[0] == order:
        order_list.pop(0)
        order+=1
    else :
        stack.append(order_list.pop(0))
    
    while stack :
        if stack[-1] == order:
            stack.pop()
            order +=1
        else :
            break
    

print('Nice') if len(stack)==0 else print('Sad')

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음.. μ €λŠ” 문제 κ·ΈλŒ€λ‘œ μ€„μ„œλŠ” 것 처럼 ν’€μ—ˆλŠ”λ° 쀑볡 μ½”λ“œλ„ 많이 생기고 κ·Έλ ‡λ„€μš”

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val n = br.readLine().toInt()
    val students = br.readLine().split(" ").map { it.toInt() }.toMutableList()

    val stack = Stack<Int>()
    var last = 0

    while (stack.isNotEmpty() || students.isNotEmpty()) {
        when {
            // 곡간이 λΉ„μ—ˆμ„ λ•Œ
            stack.isEmpty() -> {
                val student = students.removeFirst()
                if (student == last + 1) {
                    last++
                } else {
                    stack.add(student)
                }
                continue
            }
           // 쀄이 λΉ„μ—ˆμ„ λ•Œ
            students.isEmpty() -> {
                val student = stack.pop()
                if (student == last + 1) {
                    last++
                    continue
                } else {
                    println("Sad")
                    return
                }
            }
            // 곡간 맨 μ•žλ³΄λ‹€ 쀄 μ•žμ‚¬λžŒ 순번이 더 클 λ•Œ
            stack.peek() < students[0] -> {
                val student = stack.pop()
                if (student == last + 1) {
                    last++
                    continue
                } else {
                    println("Sad")
                    return
                }
            }
            //  쀄 μ•žμ‚¬λžŒ 순번이 곡간 맨 λ’€ 보닀 클 λ•Œ
            else -> {
                val student = students.removeFirst()
                if (student == last + 1) {
                    last++
                } else {
                    stack.add(student)
                }
                continue
            }
        }
    }

    println("Nice")
}

Copy link
Member

@fnzksxl fnzksxl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν—‰ μŠ€νƒμ— λ‚¨μ•„μžˆλŠ”μ§€ μ•„λ‹Œμ§€λ‘œ Nice Sadλ₯Ό κ΅¬λΆ„ν•˜μ…¨κ΅°μš”..
μ €λŠ” λ¬΄μ§€μ„±μœΌλ‘œ 비ꡐ λ•Œλ €λ²„λ ΈλŠ”λ°..
풀이 μž˜λ΄€μŠ΅λ‹ˆλ‹€

import sys

input = sys.stdin.readline

N = int(input())
line = list(map(int, input().split()))
new_line = []
detour = []
start = 0

for _next in line:
  while detour and detour[-1] == start+1:
    new_line.append(detour.pop())
    start += 1
  
  if _next == start+1:
    new_line.append(_next)
    start += 1
  else:
    detour.append(_next)
    
while detour:
  new_line.append(detour.pop())

if new_line == sorted(line):
  print("Nice")
else:
  print("Sad")

@alstjr7437
Copy link
Member

였 λ¨Όμ € 풀어보고 리뷰λ₯Ό λ‹¬λ €λŠ”λ° 거의 λΉ„μŠ·ν•˜κ΅°μš”!!

from collections import deque

n = int(input())
ticket = deque(list(map(int, input().split())))
nop = []

ticket_num = 1

while ticket :
    a = ticket.popleft()
    if ticket_num != a:
        nop.append(a)
    else :
        ticket_num = a+1
        
    for i in range(len(nop)):
        if ticket_num == nop[-1]:
            ticket_num = nop[-1] + 1
            nop.pop()
        else :
            break

if nop:
    print("Sad")
else :
    print("Nice")


while order_list :
if order_list[0] == order:
order_list.pop(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제 μœ ν˜•λ•Œλ¬Έμ— μŠ€νƒ μ“°μ‹  것 같은데 λ”°λ‘œ 문제 μœ ν˜• μ•Œμ•„λ„ 큐둜 μ¨μ„œ μ‹œκ°„ λ‚­λΉ„λ₯Ό μ€„μ΄λŠ”κ²Œ μ’‹μ•„λ³΄μž…λ‹ˆλ‹€!!!

λ”°λ‘œ μŠ€νƒμ˜ pop(0)λ₯Ό 해버리면 O(n)이 κ±Έλ¦¬μ§€λ§Œ
큐의 popleft()λ₯Ό μ‚¬μš©ν•˜λ©΄ O(1)이 κ±Έλ €μ„œ n의 μ΅œλŒ€κ°’μ΄ 크면 μ‹œκ°„μ΄ˆκ³Όκ°€ 걸릴 것 κ°™μ•„μ„œ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants