๐Ÿ•๏ธ ICPC Sinchon/Two Pointer

[BOJ][C++] ๋ฐฑ์ค€ 2467๋ฒˆ: ์šฉ์•ก

์„ ๋‹ฌ 2024. 8. 18. 04:58
๋ฐ˜์‘ํ˜•

https://www.acmicpc.net/problem/2467

 

๋ฌธ์ œ

KOI ๋ถ€์„ค ๊ณผํ•™์—ฐ๊ตฌ์†Œ์—์„œ๋Š” ๋งŽ์€ ์ข…๋ฅ˜์˜ ์‚ฐ์„ฑ ์šฉ์•ก๊ณผ ์•Œ์นผ๋ฆฌ์„ฑ ์šฉ์•ก์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋‹ค. ๊ฐ ์šฉ์•ก์—๋Š” ๊ทธ ์šฉ์•ก์˜ ํŠน์„ฑ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ•˜๋‚˜์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ ธ์žˆ๋‹ค. ์‚ฐ์„ฑ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์€ 1๋ถ€ํ„ฐ 1,000,000,000๊นŒ์ง€์˜ ์–‘์˜ ์ •์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ด๊ณ , ์•Œ์นผ๋ฆฌ์„ฑ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์€ -1๋ถ€ํ„ฐ -1,000,000,000๊นŒ์ง€์˜ ์Œ์˜ ์ •์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค.

๊ฐ™์€ ์–‘์˜ ๋‘ ์šฉ์•ก์„ ํ˜ผํ•ฉํ•œ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์€ ํ˜ผํ•ฉ์— ์‚ฌ์šฉ๋œ ๊ฐ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์˜ ํ•ฉ์œผ๋กœ ์ •์˜ํ•œ๋‹ค. ์ด ์—ฐ๊ตฌ์†Œ์—์„œ๋Š” ๊ฐ™์€ ์–‘์˜ ๋‘ ์šฉ์•ก์„ ํ˜ผํ•ฉํ•˜์—ฌ ํŠน์„ฑ๊ฐ’์ด 0์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์šฉ์•ก์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค. 

์˜ˆ๋ฅผ ๋“ค์–ด, ์ฃผ์–ด์ง„ ์šฉ์•ก๋“ค์˜ ํŠน์„ฑ๊ฐ’์ด [-99, -2, -1, 4, 98]์ธ ๊ฒฝ์šฐ์—๋Š” ํŠน์„ฑ๊ฐ’์ด -99์ธ ์šฉ์•ก๊ณผ ํŠน์„ฑ๊ฐ’์ด 98์ธ ์šฉ์•ก์„ ํ˜ผํ•ฉํ•˜๋ฉด ํŠน์„ฑ๊ฐ’์ด -1์ธ ์šฉ์•ก์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ์ด ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์ด 0์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์šฉ์•ก์ด๋‹ค. ์ฐธ๊ณ ๋กœ, ๋‘ ์ข…๋ฅ˜์˜ ์•Œ์นผ๋ฆฌ์„ฑ ์šฉ์•ก๋งŒ์œผ๋กœ๋‚˜ ํ˜น์€ ๋‘ ์ข…๋ฅ˜์˜ ์‚ฐ์„ฑ ์šฉ์•ก๋งŒ์œผ๋กœ ํŠน์„ฑ๊ฐ’์ด 0์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ํ˜ผํ•ฉ ์šฉ์•ก์„ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ๋„ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์‚ฐ์„ฑ ์šฉ์•ก๊ณผ ์•Œ์นผ๋ฆฌ์„ฑ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์ด ์ •๋ ฌ๋œ ์ˆœ์„œ๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ์ค‘ ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์šฉ์•ก์„ ํ˜ผํ•ฉํ•˜์—ฌ ํŠน์„ฑ๊ฐ’์ด 0์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์šฉ์•ก์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๋‘ ์šฉ์•ก์„ ์ฐพ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ์ „์ฒด ์šฉ์•ก์˜ ์ˆ˜ N์ด ์ž…๋ ฅ๋œ๋‹ค. N์€ 2 ์ด์ƒ 100,000 ์ดํ•˜์˜ ์ •์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” N๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ž…๋ ฅ๋˜๋ฉฐ, ์ด ์ˆ˜๋“ค์€ ๋ชจ๋‘ -1,000,000,000 ์ด์ƒ 1,000,000,000 ์ดํ•˜์ด๋‹ค. N๊ฐœ์˜ ์šฉ์•ก๋“ค์˜ ํŠน์„ฑ๊ฐ’์€ ๋ชจ๋‘ ์„œ๋กœ ๋‹ค๋ฅด๊ณ , ์‚ฐ์„ฑ ์šฉ์•ก๋งŒ์œผ๋กœ๋‚˜ ์•Œ์นผ๋ฆฌ์„ฑ ์šฉ์•ก๋งŒ์œผ๋กœ ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ํŠน์„ฑ๊ฐ’์ด 0์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์šฉ์•ก์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๋‘ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋Š” ๋‘ ์šฉ์•ก์€ ํŠน์„ฑ๊ฐ’์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ํŠน์„ฑ๊ฐ’์ด 0์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์šฉ์•ก์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋‘ ๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ์—๋Š” ๊ทธ ์ค‘ ์•„๋ฌด๊ฒƒ์ด๋‚˜ ํ•˜๋‚˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ’€์ด

์ ˆ๋Œ€๊ฐ’ ํ•จ์ˆ˜ ๊ธฐ์–ต ์•ˆ๋‚˜์„œ ๊ทธ๋ƒฅ ๊ตฌํ˜„ํ•ด์„œ ์ผ๋‹ค.

left์™€ right ์ธ๋ฑ์Šค ๋‘๊ฐœ๋ฅผ ์ง€์ •ํ•ด์„œ ๋งŒ์กฑํ•˜๋Š” ์Œ์„ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ ํˆฌํฌ์ธํ„ฐ

// ํ’€์ด : https://whkakrkr.tistory.com

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

using namespace std;

int getAbs(int num) {
    if(num<0) {
        return num*-1;
    }
    return num;
}

int main() {
    int n;
    cin >> n;
    vector<int>v(n);
    for(int i=0; i<n; i++) {
        cin >> v[i];
    }
    
    int left=0, right=n-1;
    int cur = v[left] + v[right];
    pair<int, int> ans = {left, right};
    
    while(left<right) {
        int sum = v[left] + v[right];
        
        if(getAbs(sum) < getAbs(cur)) {
            cur = sum;
            ans = {left, right};
        }
        
        if(sum<0) {
            left++;
        } else {
            right--;
        }
    }
    
    cout << v[ans.first] << " " << v[ans.second];
    
    return 0;
}
๋ฐ˜์‘ํ˜•