Skip to content

Conversation

@Fnhid
Copy link
Collaborator

@Fnhid Fnhid commented Jul 15, 2025

πŸ”— 문제 링크

μ’‹λ‹€

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„
ν…ŒμŠ€νŠΈμΌ€μ΄μŠ€κ°€ 생각보닀 λΉ‘λΉ‘ν–ˆμŠ΅λ‹ˆλ‹€ ..
μ˜ˆμ™Έμ²˜λ¦¬ 생각 잘 ν•΄μ€˜μ•Όν•¨..

✨ μˆ˜λ„ μ½”λ“œ

N개의 수 μ€‘μ—μ„œ λ‹€λ₯Έ 수 두 개의 ν•©μœΌλ‘œ 이루어진 '쒋은 수'의 개수λ₯Ό μ°ΎλŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

μ €λŠ” 투 ν¬μΈν„°λ‘œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

4
1 2 3 4 

μœ„ μΌ€μ΄μŠ€μ—μ„œ 3이 '쒋은 수'인지 검사해 λ΄…μ‹œλ‹€.

image

target<sumμ΄λ―€λ‘œ R--ν•΄μ€λ‹ˆλ‹€.

image

target 포인터와 R 포인터가 같은 곳을 가리킀고 μžˆμŠ΅λ‹ˆλ‹€. 'λ‹€λ₯Έ 두 수의 ν•©'을 ꡬ해야 ν•˜λ―€λ‘œ R--ν•΄μ€λ‹ˆλ‹€.

image

1+2=3μž„μ„ μ°Ύμ•˜μŠ΅λ‹ˆλ‹€. 3은 '쒋은 수'μž…λ‹ˆλ‹€.

이런 μ‹μœΌλ‘œ λͺ¨λ“  μˆ˜μ— 이 과정을 λ°˜λ³΅ν•΄μ£Όλ©΄ 쒋은 수의 개수λ₯Ό ꡬ할 수 μžˆμŠ΅λ‹ˆλ‹€.

μˆ˜λ„ μ½”λ“œ

μ •μˆ˜ n을 μž…λ ₯λ°›λŠ”λ‹€
n개의 μ •μˆ˜λ₯Ό μž…λ ₯λ°›μ•„ 리슀트 v에 μ €μž₯ν•œλ‹€

vλ₯Ό μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€

쒋은 수의 개수λ₯Ό μ €μž₯ν•  λ³€μˆ˜ cntλ₯Ό 0으둜 μ΄ˆκΈ°ν™”ν•œλ‹€

iλ₯Ό 0λΆ€ν„° n-1κΉŒμ§€ λ°˜λ³΅ν•˜λ©΄μ„œ λ‹€μŒμ„ μˆ˜ν–‰ν•œλ‹€:
    target에 v[i] 값을 μ €μž₯ν•œλ‹€
    μ™Όμͺ½ 포인터 l을 0으둜, 였λ₯Έμͺ½ 포인터 r을 n-1둜 μ΄ˆκΈ°ν™”ν•œλ‹€

    l이 r보닀 μž‘μ„ λ™μ•ˆ λ°˜λ³΅ν•œλ‹€:
        λ§Œμ•½ l이 i와 κ°™λ‹€λ©΄:
            l을 1 μ¦κ°€μ‹œν‚€κ³  λ‹€μŒ 반볡으둜 λ„˜μ–΄κ°„λ‹€

        λ§Œμ•½ r이 i와 κ°™λ‹€λ©΄:
            r을 1 κ°μ†Œμ‹œν‚€κ³  λ‹€μŒ 반볡으둜 λ„˜μ–΄κ°„λ‹€

        v[l]κ³Ό v[r]의 합을 sum에 μ €μž₯ν•œλ‹€

        λ§Œμ•½ sum이 targetκ³Ό κ°™λ‹€λ©΄:
            cntλ₯Ό 1 μ¦κ°€μ‹œν‚€κ³  λ°˜λ³΅λ¬Έμ„ μ’…λ£Œν•œλ‹€

        λ§Œμ•½ sum이 target보닀 μž‘λ‹€λ©΄:
            l을 1 μ¦κ°€μ‹œν‚¨λ‹€

        κ·Έλ ‡μ§€ μ•Šλ‹€λ©΄:
            r을 1 κ°μ†Œμ‹œν‚¨λ‹€

cntλ₯Ό 좜λ ₯ν•œλ‹€

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

@froglike6
Copy link
Collaborator

froglike6 commented Jul 16, 2025

μ €λŠ” 이 문제λ₯Ό 2λ…„ 전에 투 포인터λ₯Ό μ΄μš©ν•΄ ν’€μ—ˆμŠ΅λ‹ˆλ‹€(μ’‹λ‹€). 기얡이 잘 λ‚˜μ§„ μ•Šμ§€λ§Œ, λ‹€μ‹œ λ³΅μŠ΅μ„ ν•˜λ©° μ½”λ“œλ₯Ό λ³΄λ‹ˆ μœ ν˜„λ‹˜μ˜ μ„€λͺ…κ³Ό 맀우 ν‘μ‚¬ν•œ 것 κ°™μŠ΅λ‹ˆλ‹€. νŒŒμ΄μ¬μ΄λ‹€λ³΄λ‹ˆ ν™•μ‹€νžˆ νŽΈν•œ 것 κ°™μŠ΅λ‹ˆλ‹€.

import sys

N = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
arr.sort()
ans = 0

for i in range(N):
    tmp = arr[:i] + arr[i + 1:]
    left, right = 0, len(tmp) - 1
    while left < right:
        t = tmp[left] + tmp[right]
        if t == arr[i]:
            ans += 1
            break
        if t < arr[i]: left += 1
        else: right -= 1
print(ans)

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

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

μ²˜μŒμ— μ΄λΆ„νƒμƒ‰μœΌλ‘œ μƒκ°ν–ˆλ‹€κ°€ νˆ¬ν¬μΈν„°λ‘œ ν‹€μ–΄μ„œ λ§žμ·„μŠ΅λ‹ˆλ‹€.
μ•„λ¬΄λž˜λ„ μ½”λ“œλ§Œ 놓고 보면 이뢄탐색 μ½”λ“œλž‘ λΉ„μŠ·ν•œ 뢀뢄이 λ§Žμ€ 것 κ°™λ„€μš”..~!

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int A[2001];

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    
    int N, answer = 0, target;
    cin >> N;
    for(int i = 0; i < N; i++)
        cin >> A[i];
    sort(A, A + N);

    for(int i = 0; i < N; i++){
        target = A[i];
        int left = 0, right = N-1, sum;
        while(left < right){
            sum = A[left] + A[right];
            if(sum == target){ 
                if(left!= i && right != i){
                    answer++;
                    break;
                }
                else if(left== i) left++;
                else if(right == i) right--;
            }
            else if(sum < target) left++;
            else right--;
        }
    }
    cout << answer;
    return 0;
}

@dohyeondol1 dohyeondol1 removed the request for review from hadongun July 28, 2025 10:36
@Fnhid Fnhid merged commit ab67744 into main Aug 2, 2025
1 check passed
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