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-YIM2UL2ET #16

Merged
merged 2 commits into from
Feb 27, 2024
Merged

5-YIM2UL2ET #16

merged 2 commits into from
Feb 27, 2024

Conversation

YIM2UL2ET
Copy link
Collaborator

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

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

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

30m? (์ „๋‚  ํ’€์—ˆ๋˜ ๊ฑฐ๋ผ ์ž์„ธํžˆ ๊ธฐ์–ต์ด ์•ˆ ๋‚จ)

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

1. ๋ฌธ์ œ ์„ค๋ช…

์ด ๋ฌธ์ œ๋Š” ์›ํ˜• ๋ฐฐ์—ด์˜ ํฌ๊ธฐ n๊ณผ, ์ด๋™ ์นธ์ˆ˜ k๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„์„œ ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์นธ๋ถ€ํ„ฐ k์นธ์”ฉ ์ด๋™ํ•˜์—ฌ ์›์†Œ๋ฅผ ์ถœ๋ ฅ ๋ฐ ์ œ๊ฑฐํ•ด๋‚˜๊ฐ€๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

2. ๋ฌธ์ œ ํ•ด์„

์ €๋ฒˆ ๋ฌธ์ œ์™€ ๊ฑฐ์˜ ๋น„์Šทํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ ๋ฐฉ์‹๋งŒ ์กฐ๊ธˆ ์กฐ์ •ํ•ด์ฃผ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
(์ง€๋‚œ PR์„ ๋ณด๊ณ  ์™”๋‹ค๋Š” ๊ฐ€์ • ํ•˜์— ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.)

1. ์ถœ๋ ฅ ๋ฐฉ์‹

์˜ˆ์ œ์˜ ์ถœ๋ ฅ ๋ฐฉ์‹์„ ์˜ˆ๋กœ ๋“ค์–ด ๋ด…์‹œ๋‹ค. <3, 6, 2, 7, 5, 1, 4> ๋กœ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋จผ์ € "<"์„ ์ถœ๋ ฅ์„ ํ•ด์ฃผ์–ด์•ผ๊ฒ ์ง€์š”.
๊ทธ ํ›„์—” k๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ถœ๋ ฅ ํ›„ ", "์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ๋ฐ˜๋ณตํ•˜๋ฉด "<3, 6, 2, ...๊ฐ€ ๋˜๊ฒ ์ง€์š”.

ํ•˜์ง€๋งŒ ๋งˆ์ง€๋ง‰ ">"๊ฐ€ ์ถœ๋ ฅ ๋˜๋Š” ๊ณณ๊นŒ์ง€ ๊ฐ„๋‹ค๋ฉด "<3, 6, 2, 7, 5, 1, 4, >"๋กœ ์ถœ๋ ฅ์ด ๋˜๊ณ  ๋ง๊ฒ ์ง€์š”.
๋”ฐ๋ผ์„œ ๋งˆ์ง€๋ง‰์€ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
4 ์ดํ›„์— ๋” ์ด์ƒ ๋‚˜์˜ฌ ์š”์†Œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” 0์ด ๋˜์—ˆ์„ ๊ฒƒ์ด๋ฏ€๋กœ ๋ฐฐ์—ด์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ 0์ด๋ฉด ์ข…๋ฃŒํ•˜๋ฉด ๋˜๊ฒ ์ง€์š”.

๊ทธ ํ›„์—” ">"์„ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์งค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

std::cout << "<";
    while(true) {
        // ๋‹ค์Œ k๋ฒˆ์งธ ์š”์†Œ๋กœ ์ด๋™

        if (size > 0) std::cout << ", ";
        else break;
    }
    std::cout << ">";

2. ์ด๋™ ๋ฐฉ์‹

์ด ๋ฌธ์ œ๋Š” ์ด๋™ํ•˜๋Š” ๋ฐฉ์‹์ด ๋‘ ๊ฐ€์ง€ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. index ๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ด๋™ ์‹œํ‚จ๋‹ค.
  2. ์›ํ˜• ๋ฐฐ์—ด์„ queue๋กœ ๋งŒ๋“ค์–ด pop๊ณผ push๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋™์‹œํ‚จ๋‹ค.

๊ทธ ํ›„ k๋ฒˆ์งธ ์‚ฌ๋žŒ์„ ์ œ๊ฑฐํ•˜๋ฉด 1๋ฒˆ ๋ฐฉ์‹์ด๋‚˜ 2๋ฒˆ ๋ฐฉ์‹์ด๋‚˜ k+1๋ฒˆ์งธ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
(์ธ๋ฑ์Šค๋Š” ๊ทธ๋Œ€๋กœ์ธ๋ฐ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ค„์—ˆ๊ธฐ ๋•Œ๋ฌธ)
๋”ฐ๋ผ์„œ ์ดํ›„์— k-1๋ฒˆ ์ด๋™ํ•˜๋ฉด ๊ทธ ๋‹ค์Œ k๋ฒˆ์งธ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋Š” ์ดˆ๊ธฐ์— index๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•˜๋ฉด ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ฐ™๊ฒŒ ๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋™ ํ›„์—” ์ถœ๋ ฅ์„ ํ•œ ํ›„ ์ถœ๋ ฅํ–ˆ๋˜ ์š”์†Œ๋ฅผ ์—†์• ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

1๋ฒˆ ๋ฐฉ์‹์˜ ๊ฒฝ์šฐ index์— ์žˆ๋Š” ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ์ถœ๋ ฅ ํ›„ ์ œ๊ฑฐํ•ด์ฃผ๋ฉด ๋˜๊ณ ,
2๋ฒˆ ๋ฐฉ์‹์˜ ๊ฒฝ์šฐ front์— ์žˆ๋Š” ๊ฒƒ์ด k๋ฒˆ์งธ ์š”์†Œ์ด๋ฏ€๋กœ ๋งจ ์•ž์˜ ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  popํ•ด์ฃผ๋ฉด ๋˜๊ฒ ์ง€์š”.

์ฝ”๋“œ๋กœ ์ ์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

// 1๋ฒˆ ๋ฐฉ์‹
index = (index + k - 1) % vector.size(); // ์ด๋™
std::cout << vector[index];  // ์ถœ๋ ฅ
vector.erase(vector.begin() + index);  // ์ œ๊ฑฐ
// 2๋ฒˆ ๋ฐฉ์‹
for (int i = 1; i < k; i++) {  // ์ด๋™
    int temp = queue.front();
    queue.pop();
    queue.push(temp);
}
std::cout << queue.front();  // ์ถœ๋ ฅ
queue.pop();  // ์ œ๊ฑฐ

์ด์ œ ์ถœ๋ ฅ ๋ฐฉ์‹๊ณผ ์ด๋™ ๋ฐฉ์‹์„ ํ† ๋Œ€๋กœ ์ˆ˜๋„์ฝ”๋“œ๋ฅผ ์งœ๋ด…์‹œ๋‹ค.

3. ์ˆ˜๋„์ฝ”๋“œ

1๋ฒˆ ๋ฐฉ์‹์ด๋‚˜ 2๋ฒˆ ๋ฐฉ์‹์€ ์ž์ž˜ํ•˜๊ฒŒ ๋‹ค๋ฅด์ง€๋งŒ ํฐ ํ‹€์€ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค.

intํ˜• ๋ณ€์ˆ˜ n, k ์„ ์–ธ;
๋ฐฐ์—ด ์„ ์–ธ;

n, k ์ž…๋ ฅ;
๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ 1~n๊นŒ์ง€ ์ดˆ๊ธฐํ™”;

"<" ์ถœ๋ ฅ;
while(true) {
    k๋ฒˆ์งธ ์š”์†Œ๋กœ ์ด๋™;
    k๋ฒˆ์งธ ์š”์†Œ ์ถœ๋ ฅ;
    k๋ฒˆ์งธ ์š”์†Œ ์ œ๊ฑฐ;

    if (๋ฐฐ์—ด์˜ ํฌ๊ธฐ > 0) ", " ์ถœ๋ ฅ;
    else break;
}
">" ์ถœ๋ ฅ;

ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ;

4. ์ตœ์ข…์ฝ”๋“œ

// ver.1
#include <iostream>
#include <vector>

int main(void)
{
    int size, k, idx = 0;
    std::vector <int> v;

    std::cin >> size >> k;
    for (int i = 0; i < size;) v.push_back(++i);

    std::cout << "<";
    while(true) {
        idx = (idx + k - 1) % size--;
        std::cout << v[idx];
        v.erase(v.begin() + idx);

        if (size > 0) std::cout << ", ";
        else break;
    }
    std::cout << ">";
    
    return 0;
}
// ver.2
#include <iostream>
#include <queue>

int main(void)
{
    int n, k;
    std::queue <int> v;

    std::cin >> n >> k;
    for (int i = 0; i < n;) v.push(++i);

    std::cout << "<";
    while(true) {
        for (int i = 1; i < k; i++) {
            int temp = v.front();
            v.pop();
            v.push(temp);
        }
        std::cout << v.front();
        v.pop();

        if (v.size() > 0) std::cout << ", ";
        else break;
    }
    std::cout << ">";
    
    return 0;
}

5. ์ฐจ์ด์ 

ver.1 ver.2
๋ฐฐ์—ด vector ์‚ฌ์šฉ queue ์‚ฌ์šฉ
k๋ฒˆ์งธ ์š”์†Œ vector[index] queue.front()
์†Œ์š”์‹œ๊ฐ„ ์ƒ๋Œ€์ ์œผ๋กœ ์งง์Œ ์ƒ๋Œ€์ ์œผ๋กœ ๊ธบ

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

์˜ค๋Š˜์€ ์ข€ ๋งŽ์ด ์‰ฌ์šด๊ฑธ๋กœ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. (์˜ค๋Š˜ ํ’€๋ ค๊ณ  ํ–ˆ๋˜ ๋ฌธ์ œ๋“ค์ด ๋‹จ ํ•œ ๋ฌธ์ œ๋„ ํ’€๋ฆฌ์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ด์ฃ ..)
์ ๋‹ค ๋ณด๋‹ˆ ์ €๋ฒˆ ๋ฌธ์ œ์˜ ํ•˜์œ„ ํ˜ธํ™˜ ๋ฒ„์ „์ธ ๊ฒƒ ๊ฐ™๋„ค์š”.. ๋‹ค์Œ ๋ฌธ์ œ๋Š” ๋ฏธ๋ฆฌ ์–ด๋ ค์šด๊ฑธ ์ข€ ํ’€์–ด์„œ ์ƒˆ๋กœ์šด ๊ฑธ๋กœ ๊ฐ€์ ธ์˜ค๊ฒ ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@mong3125 mong3125 left a comment

Choose a reason for hiding this comment

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

๋ณด์ž๋งˆ์ž ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ƒ๊ฐ์ด ๋‚˜์„œ ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐ์„ ๋ชปํ–ˆ๋„ค์š”. ์—ฐ์‚ฐ์ด ์ฒซ๋ฒˆ์งธ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๋‹จํ•ด์„œ ์†๋„๊ฐ€ ๋น ๋ฅด์ง€๋งŒ ๋‘๋ฒˆ์งธ๊ฐ€ ๋” ์ง๊ด€์ ์ธ ๊ฒƒ ๊ฐ™์•„์š”. ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

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

โœ”๏ธ ์˜ค ์ €๋Š” ๋ฐ˜๋Œ€๋กœ ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์ด ๋ฐ”๋กœ ๋– ์˜ฌ๋ž๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™์‹œ์ผœ์„œ ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ๊ทธ ๋ถ€๋ถ„์„ ์—†์• ๋Š” ๋ฐฉ์‹์ด ๋” ์ต์ˆ™ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

โœ”๏ธ ๋ฌผ๋ก  ๋‹ค๋ฅธ๋ฐฉ๋ฒ•๋„ ๋„ˆ๋ฌด ์ข‹์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

โœ”๏ธ ์ถ”๊ฐ€์ ์œผ๋กœ "<3, 6, 2, 7, 5, 1, 4, >"๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด \b๋ฅผ ์จ๋ณด์‹œ๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”?
์ƒ๊ฐ๋ณด๋‹ค ์ข‹์€ ์„ ํƒ์ง€๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!!

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ˜

@YIM2UL2ET
Copy link
Collaborator Author

โœ”๏ธ ์˜ค ์ €๋Š” ๋ฐ˜๋Œ€๋กœ ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์ด ๋ฐ”๋กœ ๋– ์˜ฌ๋ž๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™์‹œ์ผœ์„œ ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ๊ทธ ๋ถ€๋ถ„์„ ์—†์• ๋Š” ๋ฐฉ์‹์ด ๋” ์ต์ˆ™ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

โœ”๏ธ ๋ฌผ๋ก  ๋‹ค๋ฅธ๋ฐฉ๋ฒ•๋„ ๋„ˆ๋ฌด ์ข‹์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

โœ”๏ธ ์ถ”๊ฐ€์ ์œผ๋กœ "<3, 6, 2, 7, 5, 1, 4, >"๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด \b๋ฅผ ์จ๋ณด์‹œ๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”?
์ƒ๊ฐ๋ณด๋‹ค ์ข‹์€ ์„ ํƒ์ง€๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!!

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ˜

'\b'๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ๋„ค์š”! ํ™•์‹คํžˆ ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์กฐ๊ฑด๋ฌธ ํ•˜๋‚˜๊ฐ€ ์ค„์–ด๋“œ๋Š”๊ฑฐ๋ผ ์‹œ๊ฐ„๋„ ์ค„์–ด๋“ค๊ฒ ๋„ค์š”. ์ถœ๋ ฅ์— ๊ด€ํ•ด์„œ ์ข‹์€ ์•„์ด๋””์–ด ํ•˜๋‚˜ ์–ป๊ณ ๊ฐ‘๋‹ˆ๋‹ค!

Copy link
Member

@kjs254 kjs254 left a comment

Choose a reason for hiding this comment

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

์ €๋„ ์›ํ˜• ๋ฐฐ์—ด์„ ๋ณด์ž๋งˆ์ž 1๋ฒˆ ๋ฐฉ์‹์ธ ์ธ๋ฑ์Šค์— ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์„ ํ•ด์ฃผ์–ด ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด ๋– ์˜ฌ๋ž์Šต๋‹ˆ๋‹ค.

์‹ค์ œ๋กœ ์†์œผ๋กœ ํ’€์ดํ•  ๋•Œ์—๋„ ์ธ๋ฑ์‹ฑํ•˜๋ฉฐ ์ œ๊ฑฐํ•ด ๋‚˜๊ฐ€๋Š”๊ฒŒ ํŽธํ•˜๋”๋ผ๊ตฌ์š”.

ํ•˜์ง€๋งŒ ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์ด ํ›จ์”ฌ ์ง๊ด€์ ์ด๊ณ  ๊น”๋”ํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค!!

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