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

5-H0ngJu #161

Merged
merged 2 commits into from
Mar 25, 2024
Merged

5-H0ngJu #161

merged 2 commits into from
Mar 25, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Mar 16, 2024

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

์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ

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

40๋ถ„

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

  • location : ์‚ฌ๋žŒ ์œ„์น˜
  • cnt : index ๊ธฐ์ค€ k๋ฒˆ์งธ ์นด์šดํŠธ
  1. table์— ์•‰์€ ์‚ฌ๋žŒ์ด ์—†์„ ๋•Œ๊นŒ์ง€ ์•„๋ž˜๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.
    1. k๋ฒˆ์งธ ์‚ฌ๋žŒ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด cnt๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋ฉฐ k-1(์ธ๋ฑ์Šค๊ธฐ์ค€)์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
      1. k๋ฒˆ์งธ ์‚ฌ๋žŒ์ด๋ผ๋ฉด, table์—์„œ popํ•˜๊ณ  answer์— ๋„ฃ๋Š”๋‹ค.
      2. ์‚ฌ๋žŒ์„ popํ•˜์˜€์œผ๋ฏ€๋กœ location์€ - 1
    2. k๋ฒˆ์งธ ์‚ฌ๋žŒ์ด ์•„๋‹ˆ๋ผ๋ฉด, cnt๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.

์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์ธ๊ฐ€? ํ–ˆ์ง€๋งŒ, ๋ฆฌ์ŠคํŠธ๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์„œ ๊ทธ๋ƒฅ ๋ฆฌ์ŠคํŠธ๋กœ ์ ‘๊ทผํ–ˆ๋‹ค.
๊ทธ๋Ÿฐ๋ฐ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ , ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ 'ํ'๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€๋ฆฐ๋‹ค๋Š” ๊ฒƒ์„ ๋’ค๋Šฆ๊ฒŒ ๊นจ๋‹ฌ์•˜๋‹ค.

ํ๋ฅผ ๋– ์˜ฌ๋ฆฌ์ง€ ๋ชปํ–ˆ๋˜ ์ด์œ ๋Š” ํ๊ฐ€ ์„ ์ž…์„ ์ถœ์ธ ํŠน์ง•์ด ์žˆ๋Š” ๊ฒƒ์— ๋ฐ˜ํ•ด, ์ด ๋ฌธ์ œ๋Š” ๋จผ์ € ๋“ค์–ด์˜จ ๊ฒƒ์ด ๋จผ์ € ๋‚˜๊ฐ€๋Š”๊ฐ€?์— ๋Œ€ํ•ด ์•„๋‹ˆ๋ผ๊ณ  ํŒ๋‹จํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ทธ๋Ÿฐ๋ฐ, ์กฐ๊ธˆ๋งŒ ์ƒ๊ฐํ•ด๋ณด๋ฉด index ๊ธฐ์ค€์œผ๋กœ k๋ฒˆ์งธ๊ฐ€ ์•„๋‹ˆ๋ฉด, popํ•˜๊ณ  ๋‹ค์‹œ pushํ•จ์œผ๋กœ์„œ ์ถฉ๋ถ„ํžˆ ํ๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๐Ÿ˜…๐Ÿ˜…

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

  • ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ ๋ฐฉ๋ฒ•
    ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ, ๊ฐœ๋ณ„ ์š”์†Œ๋ฅผ ๊ฐ๊ฐ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค, join()์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ด๋‹ค. = ๋‚ด์žฅํ•จ์ˆ˜๊ฐ€ ๋” ๋น ๋ฅด๋‹ค

์ฒ˜์Œ์—๋Š” ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์ถœ๋ ฅ ํฌ๋งท์„ ๋งž์ถ”๊ธฐ ์œ„ํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ–ˆ๋‹ค.

for i in range(len(solution(n, k))):
    if i == 0:
        print("<", end="")
        print(answer[i], end=", ")
    elif i == len(answer) - 1:
        print(answer[i], end="")
        print(">")
    else: print(answer[i], end=", ")

๊ทธ๋Ÿฐ๋ฐ ๊ณ„์† ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์„œ, ์ถœ๋ ฅํ•˜๋Š” ๋ถ€๋ถ„์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•˜์˜€๋”๋‹ˆ ์‚ฌ์ง„๊ณผ ๊ฐ™์€ ์‹คํ–‰ ์‹œ๊ฐ„์ด ๋‚˜์™”๊ณ , ํ†ต๊ณผ๊ฐ€ ๋˜์—ˆ๋‹ค.

print("<" + ", ".join(map(str, answer)) + ">")
์ฒซ๋ฒˆ์งธ ์ฝ”๋“œ ์‹คํ–‰์‹œ๊ฐ„ ๋‘๋ฒˆ์งธ ์ฝ”๋“œ ์‹คํ–‰์‹œ๊ฐ„

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

import sys

def input(): return sys.stdin.readline().rstrip()

N, K = map(int,input().split())

remain = [ x for x in range(1,N+1) ]
answer = []

index = 0
while remain:
    n = len(remain)
    index += K
    index = index % n 
    answer.append(remain[index-1])
    remain.pop(index-1)
    if index != 0: index -= 1
    
print("<", end="")
print(*answer, end="", sep=", ")
print(">", end="")

์ด๊ฑฐ ๊ทธ๋ƒฅ ์–ด๋ ต๊ฒŒ ํ’€์ง€์•Š์•„๋„ ๋งค๋ฒˆ ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š”๊ฑฐ pop(n)์œผ๋กœ ๋ฝ‘์•„๋„ $O(n^2)$์ด๋‹ˆ๊นŒ ํ’€๋ฆฌ๊ฒ ๋Š”๋ฐ? ํ•˜๊ณ  ๋ฐ”๋กœ ์Š› ํ•ด๋ณด๋‹ˆ ๋งž๋„ค์š”!







๋ญ”๊ฐ€ ์ด ๋ฌธ์ œ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฌ๋ฉด ๋” ์–ด๋ ค์šด ๋ฌธ์ œ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์€ ๋Š๋‚Œ์ด ํŒํŒ ๋“œ๋„ค์š”..

๊ณ ์ƒํ•˜์…จ์”๋‹ˆ๋‹ค.

๋‹ค์Œ ๋ฒˆ์—” ๊ตฌํ˜„ ๋ง๊ณ  ์ž๋ฃŒ ๊ตฌ์กฐ ์ชฝ์œผ๋กœ ๊ฐ€๋ด…์‹œ๋‹ค!!!

answer = []

solution(k)
print("<" + ", ".join(map(str, answer)) + ">")
Copy link
Member

Choose a reason for hiding this comment

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

์ œ๊ฐ€ ์“ด ๋ฐฉ๋ฒ• ๋ณด๋‹ค ๋” ๊ฐ„๊ฒฐํ•˜๊ณ  ์ข‹๋„ค์š” ๐Ÿ‘๐Ÿ‘๐Ÿ‘

return answer

n, k = map(int, input().split())
table = [i + 1 for i in range(n)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด ์ฝ”๋“œ ๊ธธ์ด๋Š” ์ฐจ์ด๊ฐ€ ์—†๋Š”๋ฐ range๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ์ค„์ˆ˜์žˆ์–ด์š”

table = list(range(1, n + 1))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์˜ค์˜ค ๊ทธ๋Ÿฌ๋„ค์š” ๐Ÿ‘๐Ÿ‘

@pknujsp
Copy link
Collaborator

pknujsp commented Mar 18, 2024

์ธ๋ฑ์Šค๋ฅผ K๋งŒํผ ๊ณ„์‚ฐํ•˜๊ณ 
๋ฆฌ์ŠคํŠธ์—์„œ ๋บธ ํ›„ ์š”์„ธํ‘ธ์Šค ์ˆœ์—ด ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ ํ•˜๋Š” ๊ฑธ

๋ฐ˜๋ณตํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ณต์žกํ•˜๊ฒŒ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ

์ฒœ์ฒœํžˆ ๊ณ ๋ฏผํ•ด๋ณด๋‹ˆ ํ๋ฅผ ๊ฐ€์ง€๊ณ 
K - 1 ๊ฐœ๋ฅผ pop()ํ•ด์„œ ๋‹ค์‹œ ๋’ค์— ์ถ”๊ฐ€ํ•˜๊ณ 
๋งจ ์•ž์— ์žˆ๋Š” ์›์†Œ๋ฅผ ์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์— ์ถ”๊ฐ€ํ•˜๋Š” ์‹์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉด ๋˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€๊ตฐ์š”.

N, K = map(int, input().split())
q = deque(range(1, N + 1))

result = []
while q:
    for _ in range(K - 1):
        q.append(q.popleft())
    result.append(q.popleft())

print(f"{'<'}{', '.join(map(str, result))}{'>'}")

N, K์˜ ํฌ๊ธฐ๊ฐ€ ์—„์ฒญ ํฌ๋ฉด ๋ถ„๋ช…ํžˆ ํ†ต๊ณผ๊ฐ€ ์•ˆ๋˜๊ฒ ๋‹ค ์‹ถ์–ด์„œ
์ฐพ์•„๋ณด๋‹ˆ ์—ญ์‹œ๋‚˜ ์กฐ๊ฑด์ด 5000์—์„œ 100,000๊นŒ์ง€ ์ƒํ–ฅ๋œ ์กฐ๊ฑด์œผ๋กœ
ํ”Œ๋ž˜ํ‹ฐ๋„˜3 ์งœ๋ฆฌ์ธ ์š”์„ธํ‘ธ์Šค 2๋ฌธ์ œ๊ฐ€ ์žˆ๋„ค์š”.

@Munbin-Lee
Copy link
Member

#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    cin >> N >> K;

    vector<int> v(N);
    iota(v.begin(), v.end(), 1);

    int index = K - 1;

    cout << '<';

    while (--N) {
        cout << v[index] << ", ";
        v.erase(v.begin() + index);

        index += K - 1;
        index %= (int) v.size();
    }

    cout << v[0] << '>';

    return 0;
}

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

ํ”Œ๋ž˜ํ‹ฐ๋„˜ ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ ํ’€๋‹ค๊ฐ€ GG์ณค์Šต๋‹ˆ๋‹ค...

์„ฑ์žฅํ•ด์„œ ์–ธ์  ๊ฐ€๋Š” PR ์˜ฌ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

@Munbin-Lee
Copy link
Member

N, K = map(int, input().split())

people = list(range(1, N + 1))
index = 0

while people:
    index += K - 1
    index %= len(people)
    print(people.pop(index))

์ด๋Ÿฐ ์‹์˜ ํ’€์ด๋Š” ์–ด๋–จ๊นŒ์š”?

@H0ngJu
Copy link
Collaborator Author

H0ngJu commented Mar 25, 2024

N, K = map(int, input().split())

people = list(range(1, N + 1))
index = 0

while people:
    index += K - 1
    index %= len(people)
    print(people.pop(index))

์ด๋Ÿฐ ์‹์˜ ํ’€์ด๋Š” ์–ด๋–จ๊นŒ์š”?

์˜คํ˜ธ.. k๋ฒˆ์งธ ์‚ฌ๋žŒ์„ ์ด๋ ‡๊ฒŒ๋„ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ตฐ์š” .. ใ„ทใ„ท!! ํ™•์‹คํžˆ ์ฝ”๋“œ๊ธธ์ด๊ฐ€ ์ค„์–ด๋“œ๋„ค์š”
์ข‹์€ ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜Ž

@H0ngJu H0ngJu merged commit 4b04141 into main Mar 25, 2024
@H0ngJu H0ngJu deleted the 5-H0ngJu branch March 25, 2024 14:48
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