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

12-YIM2UL2ET #39

Merged
merged 4 commits into from
Apr 2, 2024
Merged

12-YIM2UL2ET #39

merged 4 commits into from
Apr 2, 2024

Conversation

YIM2UL2ET
Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET commented Mar 28, 2024

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

BOJ 11723 - ์ง‘ํ•ฉ

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

40m

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

1. ๋ฌธ์ œ ์š”์•ฝ

n๊ฐœ๋งŒํผ ์ปค๋งจ๋“œ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ง‘ํ•ฉ์— ๊ด€ํ•œ ์—ฐ์‚ฐ์„ ํ•˜๋Š” ๋ฌธ์ œ.

2. ์ ‘๊ทผ ๋ฐฉ๋ฒ•

์‹œ๊ฐ„ ์ œํ•œ๊ณผ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์„ ๋ณด๋‹ˆ "์ž…์ถœ๋ ฅ ๋น ๋ฅด๊ฒŒ ํ•˜๋”๋ผ๋„ ํž˜๋“ค๊ฒ ๋‹ค"๋ผ๊ณ  ์ƒ๊ฐ.
C๋ฐฐ์—ด ์“ฐ๋Š” ํ’€์ด ๋ฐฉ๋ฒ•๋„ ์ƒ๊ฐํ–ˆ์ง€๋งŒ, ๋ญ”๊ฐ€ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋„ ์žˆ์„๊นŒ? ์‹ถ์–ด์„œ ๋ฐ”๋กœ ํƒœ๊ทธ๋ฅผ ๋ด„.
(์ฐพ์•„๋ณด๋‹ˆ ์ด๋ ‡๊ฒŒ ํ’€์–ด๋„ ๋ฌธ์ œ๋Š” ์—†๋‹ค๊ณ  ํ•จ. ๋งํฌ)

3. ๋น„ํŠธ๋งˆ์Šคํ‚น

๋œป

์ž๋ฃŒํ˜•์˜ ๋ฉ”๋ชจ๋ฆฌ์— ์‚ฌ์šฉ๋˜๋Š” ์ด์ง„์ˆ˜(BIT)๋ฅผ ํ™œ์šฉํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ๊ธฐ๋ฒ•

์—ฐ์‚ฐ์ž ์ข…๋ฅ˜

| ์—ฐ์‚ฐ์ž: or ์—ฐ์‚ฐ์ž์ด๋‹ค. ๋น„ํŠธ๋ฅผ ์„œ๋กœ ๋น„๊ตํ•˜์—ฌ ํ•˜๋‚˜๋งŒ 1์˜ ๊ฐ’์„ ๊ฐ€์ ธ๋„ 1์ด๋‹ค.

& ์—ฐ์‚ฐ์ž: and ์—ฐ์‚ฐ์ž์ด๋‹ค. ๋น„ํŠธ๋ฅผ ์„œ๋กœ ๋น„๊ตํ•˜์—ฌ ์ „๋ถ€ 1์˜ ๊ฐ’์„ ๊ฐ€์ ธ์•ผ 1์ด๋‹ค.

^ ์—ฐ์‚ฐ์ž: xor ์—ฐ์‚ฐ์ž์ด๋‹ค. ๋‘ ๊ฐœ์˜ ๋น„ํŠธ๋ฅผ ์„œ๋กœ ๋น„๊ตํ•˜์—ฌ ์„œ๋กœ ๋‹ค๋ฅด๋ฉด 1์ด๋‹ค.

~ ์—ฐ์‚ฐ์ž: not ์—ฐ์‚ฐ์ž์ด๋‹ค. ๊ฐ’์ด 1์€ 0์œผ๋กœ, 0์€ 1๋กœ ๋ฐ˜์ „๋œ๋‹ค.

<< ์—ฐ์‚ฐ์ž: ๋น„ํŠธ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์˜ฎ๊ธฐ๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด <<2 ์ด๋ฉด ๋น„ํŠธ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ๋‘ ์นธ ์˜ฎ๊ธฐ๊ณ , ์˜ค๋ฅธ์ชฝ ๋‘ ์นธ์„ 0์œผ๋กœ ์ฑ„์šด๋‹ค.

>> ์—ฐ์‚ฐ์ž: ๋น„ํŠธ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์˜ฎ๊ธฐ๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด >>3 ์ด๋ฉด ๋น„ํŠธ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์„ธ ์นธ ์˜ฎ๊ธฐ๊ณ , ์™ผ์ชฝ ์„ธ ์นธ์„ 0์œผ๋กœ ์ฑ„์šด๋‹ค.

์ง‘ํ•ฉ ๋ฌธ์ œ์—์„œ ํ™œ์šฉ

๋ฌธ์ œ์—์„œ ๋‚˜์˜จ ์ปค๋งจ๋“œ๋ฅผ ์ด์ง„์ˆ˜ ํ™œ์šฉ ๋ฐฉ์•ˆ์œผ๋กœ ์ƒ๊ฐํ•ด๋ณด์ž.
0๊ณผ 1์˜ intํ˜• ๋น„ํŠธ ํ‘œ๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
KakaoTalk_20240328_203755823

์ดˆ๊ธฐ ์ƒํƒœ: 0

add ์ปค๋งจ๋“œ: k๋ฒˆ์งธ ์นธ์ด 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค.
add

remove ์ปค๋งจ๋“œ: k๋ฒˆ์งธ ์นธ์ด 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค.
remove

check ์ปค๋งจ๋“œ: k๋ฒˆ์งธ ์นธ์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.
check

toggle ์ปค๋งจ๋“œ: k๋ฒˆ์งธ ์นธ์ด ๋ณด์ˆ˜๋ฅผ ๊ฐ€์ง€๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค.
toggle

all ์ปค๋งจ๋“œ: ์˜ค๋ฅธ์ชฝ 20๊ฐœ์˜ ์นธ์ด 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค. (m = 20)
all

empty ์ปค๋งจ๋“œ: ์ดˆ๊ธฐ ์ƒํƒœ๋กœ ๋ณ€ํ™˜ (0)

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

#include <iostream>

int main(void)
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    int n, k, bits = 0;
    std::string command;
    
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> command;
        if (command == "add") {
            std::cin >> k;
            bits |= (1<<k);
        }
        else if (command == "remove") {
            std::cin >> k;
            bits &= ~(1<<k);
        }
        else if (command == "check") {
            std::cin >> k;
            if (bits & (1<<k)) std::cout << "1\n";
            else std::cout << "0\n";
        }
        else if (command == "toggle") {
            std::cin >> k;
            bits ^= (1<<k);
        }
        else if (command == "all") bits = (1<<21)-1;
        else bits = 0;
    }
    return 0;
}

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

๋น„ํŠธ๋งˆ์Šคํ‚น์„ ๋ฐฐ์›€. ํฅ๋ฏธ๋กœ์› ๋‹ค.
์ฐธ๊ณ  ์ž๋ฃŒ

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.

์ƒˆ๋กœ ์ž˜ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค. ๋น„ํŠธ๋งˆ์Šคํฌ๋ผ๋Š”๊ฑธ ์ฒ˜์Œ๋ด์„œ ์—„์ฒญ ์ƒˆ๋กœ์šด ๋Š๋‚Œ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

๊ถ๊ธˆํ•œ ์ : ๋ฐฐ์—ด๋กœ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๋˜๊ฐ€์š”?

@YIM2UL2ET
Copy link
Collaborator Author

์ƒˆ๋กœ ์ž˜ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค. ๋น„ํŠธ๋งˆ์Šคํฌ๋ผ๋Š”๊ฑธ ์ฒ˜์Œ๋ด์„œ ์—„์ฒญ ์ƒˆ๋กœ์šด ๋Š๋‚Œ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

๊ถ๊ธˆํ•œ ์ : ๋ฐฐ์—ด๋กœ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๋˜๊ฐ€์š”?

์–ธ์–ด ์ข…๋ฅ˜์™€ ํ’€์ด ๋ฐฉ์‹๋งˆ๋‹ค ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
C++์€ ์›Œ๋‚™ ๋นจ๋ผ์„œ ๋น ๋ฅธ ์ž…์ถœ๋ ฅ ๋‘ ๋ฌธ์žฅ ๋„ฃ์–ด์ฃผ๋ฉด "all" ์ปค๋งจ๋“œ์—์„œ for๋ฌธ ๋Œ๋ฆฌ๋Š”๊ฑฐ ์•„๋‹ˆ๊ณ ์„œ์•ผ ์•ˆ ๋‚  ๊ฒƒ ๊ฐ™๋„ค์š”.
์ˆ˜๋„ ์ฝ”๋“œ๋ž€์— ์ฒจ๋ถ€ํ–ˆ๋˜ ๋ฐฐ์—ดํ’€์ด๋ฐฉ์‹ ๋งํฌ์ž…๋‹ˆ๋‹ค. ์ฐธ๊ณ ํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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.

์‹ ๊ธฐํ•œ ๋ฐฉ๋ฒ•์ด๋„ค์š”!!

์•ฝ๊ฐ„์€ ์ดํ•ด๊ฐ€ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์ด ์žˆ์–ด ์ถ”๊ฐ€ ์งˆ๋ฌธ์„ ๋“œ๋ฆฝ๋‹ˆ๋‹ค.
์ฝ”๋“œ ์ƒ์—์„œ๋Š” ์ˆซ์ž๋กœ์„œ ํ‘œํ˜„๋˜๊ฒ ์ง€๋งŒ, bit๋ฅผ ๋ฐฐ์—ด๋กœ์„œ ์ƒ๊ฐํ•ด๋ณธ๋‹ค๋ฉด, ๊ฐ index์— ๋Œ€ํ•˜์—ฌ 0์ด ๋“ค์–ด์˜ค๋ฉด ๊ฐ’์ด ์—†๋Š” ๊ฒƒ์œผ๋กœ, 1์ด ๋“ค์–ด์˜ค๋ฉด ๊ฐ’์ด ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์ธ์ง€ ํ™•์ธ์ฐจ ์งˆ๋ฌธ๋“œ๋ฆฝ๋‹ˆ๋‹ค

๋˜๊ฒŒ ์žฌ๋ฐŒ๋Š” ๋ฐฉ๋ฒ•์ด๋„ค์š”. ๋‹ค์Œ๋ฒˆ์— ์จ๋จน์„ ๊ธฐํšŒ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•œ๋ฒˆ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ˜

@YIM2UL2ET
Copy link
Collaborator Author

์ฝ”๋“œ ์ƒ์—์„œ๋Š” ์ˆซ์ž๋กœ์„œ ํ‘œํ˜„๋˜๊ฒ ์ง€๋งŒ, bit๋ฅผ ๋ฐฐ์—ด๋กœ์„œ ์ƒ๊ฐํ•ด๋ณธ๋‹ค๋ฉด, ๊ฐ index์— ๋Œ€ํ•˜์—ฌ 0์ด ๋“ค์–ด์˜ค๋ฉด ๊ฐ’์ด ์—†๋Š” ๊ฒƒ์œผ๋กœ, 1์ด ๋“ค์–ด์˜ค๋ฉด ๊ฐ’์ด ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์ธ์ง€ ํ™•์ธ์ฐจ ์งˆ๋ฌธ๋“œ๋ฆฝ๋‹ˆ๋‹ค

๋งž์Šต๋‹ˆ๋‹ค. 0(false)์ด๋ฉด ๊ทธ index์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋‹ค๋Š” ๋œป์ด๊ณ , 1(true)์ด๋ฉด ์กด์žฌํ•œ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค.

@YIM2UL2ET YIM2UL2ET merged commit 95b4a96 into main Apr 2, 2024
1 check passed
@YIM2UL2ET YIM2UL2ET deleted the 12-YIM2UL2ET branch April 2, 2024 12:02
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.

3 participants