๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Problem Solving/BAEKJOON

[baekjoon]python #10773 ์ œ๋กœ

by DevIseo 2022. 6. 23.

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

[baekjoon]python #10773 ์ œ๋กœ

 

10773๋ฒˆ: ์ œ๋กœ

์ฒซ ๋ฒˆ์งธ ์ค„์— ์ •์ˆ˜ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ K ≤ 100,000) ์ดํ›„ K๊ฐœ์˜ ์ค„์— ์ •์ˆ˜๊ฐ€ 1๊ฐœ์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ •์ˆ˜๋Š” 0์—์„œ 1,000,000 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ, ์ •์ˆ˜๊ฐ€ "0" ์ผ ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์žฅ ์ตœ๊ทผ์— ์“ด ์ˆ˜๋ฅผ ์ง€์šฐ๊ณ , ์•„๋‹ ๊ฒฝ

www.acmicpc.net

์ฒ˜์Œ์— ์‹œ๋„ํ–ˆ๋˜ ์ฝ”๋“œ -- ์‹œ๊ฐ„ ์ดˆ๊ณผ!๐Ÿ˜ฅ

n=int(input())
stack=[]
for i in range(n):
    temp = int(input())
    if temp !=0:
        stack.append(temp)
    else:
        stack=stack[0:len(stack)-1]

print(sum(stack))

๋‘๋ฒˆ์งธ๋กœ ์‹œ๋„ํ–ˆ๋˜ ์ฝ”๋“œ - ์„ฑ๊ณต!

์ฒซ๋ฒˆ์งธ ์ฝ”๋“œ์—์„œ sys.stdin.readline์œผ๋กœ ์ž…๋ ฅ์„ ๋ฐ›์•˜๋‹ค. ํ•˜์ง€๋งŒ 5000ms๋ผ๋Š” ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๋‹ค.

import sys
input = sys.stdin.readline
n=int(input())
stack=[]
for i in range(n):
    temp = int(input())
    if temp !=0:
        stack.append(temp)
    else:
        stack=stack[0:len(stack)-1]

print(sum(stack))

์„ธ๋ฒˆ์งธ๋กœ ์‹œ๋„ํ•œ ์ฝ”๋“œ - ์ •๋‹ต!

128ms!

import sys
input = sys.stdin.readline
n=int(input())
stack=[]
sumV=0
for i in range(n):
    temp = int(input())
    if temp !=0:
        stack.append(temp)
        sumV+=temp
    else:
        sumV-=stack[-1]
        stack.pop()
print(sumV)

input๋ฐ›๋Š” ์ˆซ์ž๋ฅผ temp์— ๋„ฃ์–ด์ฃผ๊ณ  0์ผ๋•Œ์™€ ์•„๋‹๋•Œ๋ฅผ ๊ตฌ๋ถ„ํ•ด stack์— ๋„ฃ์–ด์ฃผ๊ณ  ๋นผ๋Š” ์ž‘์—…์„ ํ–ˆ๋‹ค.

์œ„์˜ ์ฝ”๋“œ๋“ค๊ณผ ๋‹ค๋ฅธ ์ ์€ sumV๋ฅผ ํ†ตํ•ด ๊ณ„์‚ฐ์˜ ์†๋„๋ฅผ ๋†’์˜€๋‹ค๋Š” ์ ์ด๋‹ค!

๊ทธ๋ž˜์„œ ๋‘๋ฒˆ์งธ ์ฝ”๋“œ๋ณด๋‹ค ์•ฝ 1/25์ •๋„๋กœ ์‹œ๊ฐ„์„ ๋‹จ์ถ•ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๋Œ“๊ธ€