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번 - 같이 λˆˆμ‚¬λžŒ λ§Œλ“€λž˜? λ¬Έμ œκ°€ μž¬μ±„μ  λ˜μ—ˆμŠ΅λ‹ˆλ‹€. (μž¬μ±„μ  이유: 데이터 μΆ”κ°€)" λ³΄λŠ” κ±ΈκΉŒμš” γ…‹γ…‹γ…‹

Copy link
Collaborator

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

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

였호 이 λ¬Έμ œμ—μ„œ νˆ¬ν¬μΈν„°λŠ” μ–‘μͺ½ λμ—μ„œ μ’ν˜€μ˜€λ„€μš”!!
μ• λ²Œλ ˆμ²˜λŸΌ ν•œ λ°©ν–₯으둜 μ›€μ§μ΄λŠ” νˆ¬ν¬μΈν„° 문제λ₯Ό 자주 ν’€μ–΄μ„œ μ–‘μͺ½ 끝은 μ–΄λ–»κ²Œ ν–ˆμ—ˆμ§€ν•˜λ©΄μ„œ λ”λ“¬κ±°λ ΈμŠ΅λ‹ˆλ‹€ γ…‹γ…‹γ…‹ πŸ™„

μ½”λ“œ 보기

import java.io.*;
import java.util.*;

public class Main {
    static int N; 
    static int snow[]; 
    static int min = Integer.MAX_VALUE;  

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());

        snow = new int[N];
        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
	        snow[i] = Integer.parseInt(st.nextToken());
        }
        Arrays.sort(snow);

        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
                int snowMan1 = snow[i] + snow[j];

                int start = 0;
                int end = N - 1;

                while (start < end) {
                    if (start == i || start == j) {
                        start++;
                        continue;
                    }

                    if (end == i || end == j) {
                        end--;
                        continue;
                    }

                    int snowMan2 = snow[start] + snow[end];
                    min = Math.min(min, Math.abs(snowMan1 - snowMan2));

                    if (snowMan1 > snowMan2) start++;
                    else if (snowMan1 < snowMan2) end--;
                    else {
                        System.out.println(0);
                        return;
                    }
                }
            }
        }
        System.out.println(min);
    }
}

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