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

43-gjsk132 #234

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

43-gjsk132 #234

wants to merge 1 commit into from

Conversation

gjsk132
Copy link
Member

@gjsk132 gjsk132 commented Nov 3, 2024

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

๋ฐฑ์ค€ 20366 : ๊ฐ™์ด ๋ˆˆ์‚ฌ๋žŒ ๋งŒ๋“ค๋ž˜?

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

1์‹œ๊ฐ„ 30๋ถ„

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

๐ŸŽฏ ๋ฌธ์ œ ์ดํ•ด

๋ฌธ์ œ ์š”์•ฝ
4๊ฐœ๋ฅผ ์„ ํƒํ•ด์„œ ๋ˆˆ์‚ฌ๋žŒ 2๊ฐœ๋ฅผ ๋งŒ๋“ ๋‹ค.
2๊ฐœ์˜ ์ง€๋ฆ„์˜ ํ•ฉ์ด ๋ˆˆ์‚ฌ๋žŒ์˜ ํ‚ค์ด๋‹ค.
2๊ฐœ์˜ ๋ˆˆ์‚ฌ๋žŒ์˜ ํ‚ค ์ฐจ์ด๊ฐ€ ์ตœ์†Œ์ธ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ผ.

์ฒ˜์Œ์—๋Š” 4๊ฐœ๋ฅผ ๋‹ค ๊ณจ๋ผ์ค˜์•ผํ•˜๋‚˜ ์‹ถ์—ˆ๋Š”๋ฐ...

๊ทธ๋ƒฅ 2๊ฐœ๋Š” ๋ฏธ๋ฆฌ ๊ณจ๋ผ์„œ target์œผ๋กœ ์ •ํ•˜๊ณ , ๋‚˜๋จธ์ง€ 2๊ฐœ๋ฅผ ํˆฌํฌ์ธํ„ฐ๋กœ ๋Œ๋ฉด์„œ target๊ฐ’๊ณผ ๊ฐ€๊นŒ์›Œ์ง€๋„๋ก ํ•˜์˜€๋‹ค.
์‚ฌ์šฉํ•œ ๋ˆˆ๋ฉ์ด๋Š” ์“ฐ๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•œ ๋ˆˆ๋ฉ์ด์™€ ๊ฐ™์€ idx๋กœ ๊ฐ€๋ฉด ์Šคํ‚ตํ•ด์ค€๋‹ค.

๐Ÿ”์ฝ”๋“œ ์„ค๋ช…

์ฃผ์š” ์ฝ”๋“œ

๊ฐ€์žฅ ํ•ต์‹ฌ์ด ๋˜๋Š” ํˆฌํฌ์ธํ„ฐ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

2๊ฐœ๋กœ ์„ ์ •ํ•œ ๊ฐ’์„ block1, block2๋กœ ์ •ํ•˜๊ณ , ํ•ด๋‹น ์œ„์น˜์˜ ๋ˆˆ๋ฉ์ด ์ง€๋ฆ„์˜ ํ•ฉ์„ target์œผ๋กœ ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค.

์ตœ์†Œ ์ฐจ์ด๋ฅผ target์œผ๋กœ ํ•ด๋‘๊ณ , start์™€ end๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค์ค๋‹ˆ๋‹ค.

๋งŒ์•ฝ block1๊ณผ block2๋ž‘ ๊ฒน์นœ๋‹ค๋ฉด start๋ฉด +1, end๋ฉด -1์„ ํ•ด์ค๋‹ˆ๋‹ค.

์ดํ›„์—๋Š” start์™€ end๋กœ ๋งŒ๋“  ๋ˆˆ์‚ฌ๋žŒ์˜ ํ‚ค๋ฅผ target๊ณผ ๋น„๊ตํ•ด์„œ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋๋‚˜๊ณ  ๋‚˜๋ฉด ๊ฐ€์žฅ ์ž‘์€ ํ‚ค ์ฐจ์ด๋ฅผ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

def two_pointer(block1, block2):
    start, end = 0, n-1

    target = snow[s1] + snow[s2]

    min_gap = target

    while not start == end:
        if start == block1 or start == block2:
            start += 1
            continue
        
        if end == block1 or end == block2:
            end -= 1
            continue
        
        height = snow[start] + snow[end]

        min_gap = min(min_gap, abs(target-height))

        if height > target:
            end -= 1
        else:
            start += 1
    return min_gap

์œ„ ํˆฌํฌ์ธํ„ฐ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ
2์ค‘ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ณ ์ •ํ•˜๋ ค๋Š” 2๊ฐœ์˜ ๋ˆˆ๋ฉ์ด๋ฅผ ์ •ํ•ด์ฃผ๊ณ , ์ตœ์†Œ๊ฐ’์„ answer์— ์ •ํ•ด์ค๋‹ˆ๋‹ค.

์‹œ๊ฐ„์„ ์ค„์ด๊ธฐ ์œ„ํ•ด answer์ด 0์ด ๋˜๋Š” ๊ฐ’์ด ๋‚˜์˜ค๋ฉด ๋ฐ˜๋ณต์„ ์ค‘๋‹จํ•˜๊ณ , ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

answer = snow[0] + snow[n-1]

for s1 in range(n-1):

    if not answer:
        break

    for s2 in range(s1+1, n):
        answer = min(answer, two_pointer(s1, s2))

        if not answer:
            break

print(answer)
์ „์ฒด ์ฝ”๋“œ

input = open(0).readline

n = int(input())

snow = sorted(list(map(int,input().split())))

min_gap_height = float("inf")

target = 0

def two_pointer(block1, block2):
    start, end = 0, n-1

    target = snow[s1] + snow[s2]

    min_gap = target

    while not start == end:
        if start == block1 or start == block2:
            start += 1
            continue
        
        if end == block1 or end == block2:
            end -= 1
            continue
        
        height = snow[start] + snow[end]

        min_gap = min(min_gap, abs(target-height))

        if height > target:
            end -= 1
        else:
            start += 1
    return min_gap


answer = snow[0] + snow[n-1]

for s1 in range(n-1):

    if not answer:
        break

    for s2 in range(s1+1, n):
        answer = min(answer, two_pointer(s1, s2))

        if not answer:
            break

print(answer)

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

์ตœ๊ทผ์— ํˆฌํฌ์ธํ„ฐ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ์ ‘ํ–ˆ๋Š”๋ฐ, ์ž์ฃผ ์•ˆ ํ’€์–ด๋ด์„œ ํ•œ ๋ฒˆ ํ’€์–ด๋ด…๋‹ˆ๋‹ด
๊ทธ๋ž˜๋„ ์•„์ง ์‘์šฉํ•˜๋ ค๊ณ  ํ•˜๋‹ˆ๊นŒ ์•ฝ๊ฐ„ ์–ด๋ ต๋„ค์š” ํ›„์Œ ๐Ÿค”

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

์ €๋„ ์ฒ˜์Œ์—” "๋ญ์ง€ ํฌ์ธํ„ฐ 4๊ฐœ๋ฅผ ๊ฐ–๊ณ  ๋†€์•„์•ผ ํ•˜๋‚˜?" ์ด ์ƒ๊ฐํ–ˆ๋‹ค๊ฐ€ ์—˜์‚ฌ ๋ˆˆ๋ฉ์ด๋Š” 2์ค‘ for๋ฌธ์œผ๋กœ naiveํ•˜๊ฒŒ ๋Œ๋ฆฌ๊ณ  ์•ˆ๋‚˜ ๋ˆˆ๋ฉ์ด 2๊ฐœ์— ๋Œ€ํ•ด์„œ ํˆฌํฌ์ธํ„ฐ๋ฅผ ๊ตด๋ฆฌ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.
image

๊ทธ๋Ÿฐ๋ฐ ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๊ตฐ์š”. ์ง์ž‘์€ ํ–ˆ์ง€๋งŒ...

๊ทธ๋ž˜์„œ ๊ฐ€๋Šฅํ•œ ๋ˆˆ์‚ฌ๋žŒ ์กฐํ•ฉ์„ ๋ฏธ๋ฆฌ ๋ฐฐ์—ด์— ์ €์žฅํ•ด๋‘” ๋’ค, ๊ฑฐ๊ธฐ์„œ ํˆฌํฌ์ธํ„ฐ๋ฅผ ๋Œ๋ฆฌ๋Š” ์‹์œผ๋กœ ๊ณ ์ณ๋ณด๋‹ˆ๊นŒ ์‹œ๊ฐ„์ด ํ™• ๋นจ๋ผ์ง€๋„ค์š” :)

๋ณ€๊ฒฝ๋œ ์ฝ”๋“œ

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct FSnowman
{
    int Height;
    int FirstSnowball;
    int SecondSnowball;
    
    FSnowman(int InHeight, int InFirstSnowball, int InSecondSnowball)
    : Height(InHeight)
    , FirstSnowball(InFirstSnowball)
    , SecondSnowball(InSecondSnowball)
    {}
    
    bool operator<(const FSnowman& InSnowman) const
    {
        return this->Height < InSnowman.Height;
    }
};

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    int SnowballCount; cin >> SnowballCount;
    vector<int> SnowballDiameters(SnowballCount); for(int& Diameter : SnowballDiameters) cin >> Diameter;
    
    vector<FSnowman> Snowmans;
    for(int i = 0; i < SnowballCount - 1; ++i)
    {
        for(int j = i + 1; j < SnowballCount; ++j)
        {
            Snowmans.emplace_back(SnowballDiameters[i] + SnowballDiameters[j], i, j);
        }
    }
    
    sort(Snowmans.begin(), Snowmans.end());
    
    int MinSnowmanHeightDifference = 1e9;
    for(int Elsa = 0; Elsa < Snowmans.size() - 1; ++Elsa )
    {
        int Anna = Elsa + 1;
        
        while(Anna < Snowmans.size() 
        && (Snowmans[Elsa].FirstSnowball == Snowmans[Anna ].FirstSnowball
        || Snowmans[Elsa].FirstSnowball == Snowmans[Anna].SecondSnowball
        || Snowmans[Elsa].SecondSnowball == Snowmans[Anna].FirstSnowball
        || Snowmans[Elsa].SecondSnowball == Snowmans[Anna].SecondSnowball))
        {
            Anna++;
        }
        
        if(Anna < Snowmans.size())
        {
            MinSnowmanHeightDifference = min(MinSnowmanHeightDifference, 
            Snowmans[Anna].Height - Snowmans[Elsa].Height);
        }
    }
    
    cout << MinSnowmanHeightDifference;

    return 0;
}

image


target = snow[s1] + snow[s2]

min_gap = target
Copy link
Collaborator

Choose a reason for hiding this comment

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

์š”๊ฑฐ ์ดˆ๊ธฐ๊ฐ’์„ ์ด๋Ÿฐ ์‹์œผ๋กœ ์„ค์ •ํ•˜๋ฉด

4
1 2 3 999

answer: 995
output: 3

์ด๋Ÿฐ ์‹์œผ๋กœ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹น. ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •์„ ๋‹ค์‹œ ๊ณ ๋ฏผํ•ด๋ณผ๊นŒ์š”!?

Copy link
Member Author

Choose a reason for hiding this comment

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

์˜คํ˜ธ ๊ฐ์‚ฌํ•จ๋‹ˆ๋‹น

min_gap = float('inf')

๊ทผ๋ฐ ์ด์ •๋„๋ฉด ์น˜๋ช…์ ์ธ๋ฐ ์–ด์บ ๋งž์ท„์ง€ ํ…Œ์ผ€ ์ถ”๊ฐ€ํ•ด๋‹ฌ๋ผ์นผ๊นŒ์š”๐Ÿซ 

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang Nov 6, 2024

Choose a reason for hiding this comment

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

์ด์ œ ๊ณง "20366๋ฒˆ - ๊ฐ™์ด ๋ˆˆ์‚ฌ๋žŒ ๋งŒ๋“ค๋ž˜? ๋ฌธ์ œ๊ฐ€ ์žฌ์ฑ„์  ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. (์žฌ์ฑ„์  ์ด์œ : ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€)" ๋ณด๋Š” ๊ฑธ๊นŒ์š” ใ…‹ใ…‹ใ…‹

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.

2 participants