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

7-wkdghdwns199 #34

Merged
merged 3 commits into from
Feb 24, 2024
Merged

7-wkdghdwns199 #34

merged 3 commits into from
Feb 24, 2024

Conversation

wkdghdwns199
Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 commented Feb 20, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

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

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„ - ์ด๊ฒŒ ๋ฌด์Šจ ๋ฌธ์ œ์—ฌ..?? ํ•˜๊ณ  ๊ณ ๋ฏผํ•œ ์‹œ๊ฐ„์ด๋ž‘ ์ฝ”๋“œ ๋ณด๊ณ  ์ดํ•ดํ•˜๋Š” ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค..!

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ๋ณด๋ฉด ์ •๋ง ๋ฌด์Šจ ๋ง์ธ์ง€ ์ดํ•ด๊ฐ€ ๊ฐ€์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋ด๋„ ์–ด๋–ป๊ฒŒ ์ €๋Ÿฐ ์ถœ๋ ฅ์ด ๋‚˜์˜ค์ง€..? ํ•˜๋Š” ์‹ฌ์ •์œผ๋กœ ๋ณด๋‹ค๊ฐ€, ๊ฒฐ๊ตญ ๋ฌธ์ œ ํ•ด์„ค์„ ๋ณด๊ณ  ์ดํ•ด๋ฅผ ํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค..

๊ฒฐ๊ตญ ์ด ๋ฌธ์ œ๋Š” stack ์—์„œ ๋‚˜์˜ค๋Š” pop ๊ฐ’์ด๋‚˜, queue ์—์„œ ๋‚˜์˜ค๋Š” pop ๊ฐ’์ด ๋ฐ”๋กœ ์˜†์˜ stack์ด๋‚˜ pop ์— ๋“ค์–ด๊ฐ€์„œ, ๊ฒฐ๊ตญ ๋งˆ์ง€๋ง‰์— ์–ด๋– ํ•œ ๊ฐ’์ด pop ๋˜๋Š”๊ฐ€๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค!

0 1 1 0 -> ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ queue, stack, stack, queue ์ด๋‹ค.
1 2 3 4 -> ๊ฐ ์ž๋ฃŒ๊ตฌ์กฐ์— 1,2,3,4 ๊ฐ€ ์žˆ๋‹ค.

2 4 7 -> 2, 4, 7 ๋ฅผ ์ฒซ ๋ฒˆ์งธ ์ž๋ฃŒ๊ตฌ์กฐ queue ์ž๋ฃŒ๊ตฌ์กฐ์— ๋„ฃ์„ ๊ฒƒ์ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  2 ๋ฅผ ์ฒซ๋ฒˆ์งธ ์ž๋ฃŒ๊ตฌ์กฐ์ธ queue ์— ๋„ฃ์œผ๋ฉด

2๋Š” 1์„ ๋ฐ€์–ด๋‚ด๊ณ , 
1์€ ๋งจ ์œ„์—์„œ ํŠ•๊ฒจ๋‚˜๊ณ ,
1์€ ๋˜ ๋‹ค์‹œ ๋งจ ์œ„์—์„œ ํŠ•๊ฒจ๋‚˜๊ณ ,
1์€ 4๋ฅผ ๋ฐ€์–ด๋‚ด๋ฏ€๋กœ 

๋งˆ์ง€๋ง‰ pop ๊ฐ’์€ 4์ด๋‹ค.

์ด๊ฑธ ์ด์ค‘ for ๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚  ๊ฒƒ์ด ๋ถ„๋ช…ํ•ฉ๋‹ˆ๋‹ค. ( O(100000 ^ 2) ) ๊ทธ๋ž˜์„œ ํ•„์š”ํ•œ 2๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ์Šคํƒ์€ ์–ด์ฐจํ”ผ ํŠ•๊ฒจ๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ์‹ ๊ฒฝ ์“ธ ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค!
  2. queue์—์„œ ๋ฐ€๋ ค๋‚˜๋ฉด ๋‹ค์‹œ ์œ„๋กœ ์ง‘์–ด๋„ฃ๊ธฐ ๋•Œ๋ฌธ์—, queue๋“ค์€ ํ•˜๋‚˜์˜ queue ๋กœ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค!

๊ทธ๋ฆฌํ•˜๋ฉด, ์ผ๋‹จ ๋ฐ€๋ ค๋‚  ์˜ˆ์ •์ธ queue ๋“ค์˜ ์ˆซ์ž๋“ค์„ ์ผ๋‹จ ํ•˜๋‚˜์˜ queue ์— ์ง‘์–ด๋„ฃ๊ณ , ๊ทธ๋ฆฌ๊ณ  ์ง‘์–ด๋„ฃ์„ ์ˆซ์ž๋“ค์„ queue์— ๋„ฃ๊ณ  ๊ทธ ์ˆ˜ ๋งŒํผ ์ถœ๋ ฅํ•˜๋ฉด ํ•ด๊ฒฐ์ด ๋ฉ๋‹ˆ๋‹ค!

from collections import deque

N = int(input())
queue_stack = list(map(int, input().split()))
data = list(map(int, input().split()))
n = int(input())
numbers = list(map(int, input().split()))

answer = deque()

for i in range(N) :
    if queue_stack[i] == 0:
        answer.append(data[i])

for number in numbers :
    answer.appendleft(number)


for _ in range(n) :
    print(answer.pop(), end=' ')

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

@alstjr7437
Copy link
Member

์ด๊ฑธ ์ด์ค‘ for ๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚  ๊ฒƒ์ด ๋ถ„๋ช…ํ•ฉ๋‹ˆ๋‹ค. ( O(100000 ^ 2) )

ํ‘ํ‘ ๊ทธ๊ฒŒ ์ €์ž…๋‹ˆ๋‹ค..

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

m = int(input())
c = list(map(int, input().split()))

for i in range(m):
    for j in range(len(a)):
        if a[j] == 0 :
            temp = c[i]
            c[i] = b[j]
            b[j] = temp
            # print(c[i], b)

for i in c:
    print(i, end=" ")

ํ‘ํ‘ ๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ ์ƒ๊ฐํ•ด์„œ ํ™์ค€๋‹˜์ด๋ž‘ ๋˜‘๊ฐ™์€ ์•„์ด๋””์–ด์ธ ๋ฐฉ์‹์ด ๋‚˜์™”๋„ค์š”!
์ฝ”๋“œ

image

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 queueStack = br.readLine().split(" ").map { it.toInt() }
    val insertedNumber = br.readLine().split(" ").map { it.toInt() }
    var count = br.readLine().toInt()
    val arr = br.readLine().split(" ").map { it.toInt() }
    val answer = mutableListOf<Int>()

    for (i in n - 1 downTo 0) {
        if (count == 0) {
            break
        }
        if (queueStack[i] == 1) {
            continue
        }
        answer.add(insertedNumber[i])
        count--
    }

    answer.addAll(arr.subList(0, count))
    println(answer.joinToString (" "))
}

์Šคํƒ์€ ๋ฌด์‹œํ•˜๊ณ  ๋’ค์—์„œ๋ถ€ํ„ฐ ์นด์šดํŒ…ํ•ด์„œ
| 4 1 2 | 4 7 ๋„ฃ๊ณ  3๊ฐœ๋งŒํผ ์ž๋ฅด๋Š”๊ฑธ๋กœ ํ’€์—ˆ๋„ค์šฉ

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.

๋ฌธ์ œ ์ž์ฒด๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ์— ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ฆฌ๋Š” ๋ฌธ?์ œ๋„ค์š”.
์Šคํƒ์€ ๋“ค์–ด๊ฐ€๋ดค์ž ์˜๋ฏธ ์—†๊ณ , ํ๋“ค์€ ํ๋“ค์„ ์ด์–ด์„œ ํ•˜๋‚˜์˜ ํ๋กœ ์ƒ๊ฐํ•˜๋Š” ๊ฒŒ ํฌ์ธํŠธ์ธ ๋ฌธ์ œ์˜€๊ตฐ์š”..

@9kyo-hwang
Copy link

์™œ draft์ธ PR์ธ๋ฐ ๋ฆฌ๋ทฐ๊ฐ€ ์™„๋ฃŒ๋œ๊ฑฐ์ฃ  ใ…‹ใ…‹ใ…‹

@wkdghdwns199
Copy link
Collaborator Author

๋ฌธ์ œ ์ž์ฒด๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ์— ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ฆฌ๋Š” ๋ฌธ?์ œ๋„ค์š”. ์Šคํƒ์€ ๋“ค์–ด๊ฐ€๋ดค์ž ์˜๋ฏธ ์—†๊ณ , ํ๋“ค์€ ํ๋“ค์„ ์ด์–ด์„œ ํ•˜๋‚˜์˜ ํ๋กœ ์ƒ๊ฐํ•˜๋Š” ๊ฒŒ ํฌ์ธํŠธ์ธ ๋ฌธ์ œ์˜€๊ตฐ์š”..
asdas

๋ฌธ์ œ ์ž์ฒด๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ์— ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ฆฌ๋Š” ๋ฌธ?์ œ๋„ค์š”. ์Šคํƒ์€ ๋“ค์–ด๊ฐ€๋ดค์ž ์˜๋ฏธ ์—†๊ณ , ํ๋“ค์€ ํ๋“ค์„ ์ด์–ด์„œ ํ•˜๋‚˜์˜ ํ๋กœ ์ƒ๊ฐํ•˜๋Š” ๊ฒŒ ํฌ์ธํŠธ์ธ ๋ฌธ์ œ์˜€๊ตฐ์š”..

๋งž์Šด๋‹ค. ๋ฌธ์ œ ์ดํ•ดํ•˜๋Š”๋ฐ๋งŒ ํ•œ ์„ธ์›” ๊ฑธ๋ฆฌ๋Š” ๋ฌธ์ œ์—ฌ์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ์ฝ์„ ๋•Œ ์ง‘์ค‘ํ•ด์„œ ์ฝ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฑธ ๋งŽ์ด ๋Š๋‚๋‹ˆ๋‹ค ใ…‹ใ…‹ใ…‹

@wkdghdwns199 wkdghdwns199 marked this pull request as ready for review February 24, 2024 05:12
@wkdghdwns199 wkdghdwns199 merged commit 5e25bf4 into main Feb 24, 2024
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.

5 participants