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

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

์„ ๋‹ฌ 2024. 9. 4. 06:05
๋ฐ˜์‘ํ˜•

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

 

๋ฌธ์ œ

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

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

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

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

์ž…๋ ฅ

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

์ถœ๋ ฅ

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

 

ํ’€์ด

์–ด๋””์„ ๊ฐ€ ๋งŽ์ด ๋ณธ ๋ฌธ์ œ๋‹ค

[๐Ÿ•๏ธ ICPC Sinchon/Two Pointer] - [BOJ][C++] ๋ฐฑ์ค€ 2467๋ฒˆ: ์šฉ์•ก

 

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

https://www.acmicpc.net/problem/2467 ๋ฌธ์ œKOI ๋ถ€์„ค ๊ณผํ•™์—ฐ๊ตฌ์†Œ์—์„œ๋Š” ๋งŽ์€ ์ข…๋ฅ˜์˜ ์‚ฐ์„ฑ ์šฉ์•ก๊ณผ ์•Œ์นผ๋ฆฌ์„ฑ ์šฉ์•ก์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋‹ค. ๊ฐ ์šฉ์•ก์—๋Š” ๊ทธ ์šฉ์•ก์˜ ํŠน์„ฑ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ•˜๋‚˜์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ ธ์žˆ๋‹ค. ์‚ฐ์„ฑ

whkakrkr.tistory.com

 

๋‹ค ๋˜‘๊ฐ™๊ณ  ๋”ฑ ๋‘๊ฐ€์ง€๊ฐ€ ๋‹ค๋ฅด๋‹ค

1. ์ž…๋ ฅ ๊ฐ’์ด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š์Œ

2. ๋‘๊ฐœ๊ฐ€ ์•„๋‹Œ ์„ธ๊ฐœ์˜ ์šฉ์•ก ์กฐํ•ฉ์œผ๋กœ ์ฐพ์•„์•ผํ•จ

 

1๋ฒˆ์€ ์ž…๋ ฅ๋ฐ›์€ ๋ฒกํ„ฐ๋ฅผ sort๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐ๋˜๋‹ˆ ๋„˜์–ด๊ฐ€์ž.

 

2๋ฒˆ์ด ๋ฌธ์ œ์ธ๋ฐ, ๊ธฐ์กด ๋ฌธ์ œ์—์„œ๋Š” left์™€ right ํฌ์ธํ„ฐ๋ฅผ ๋‘๊ฐœ๋ฅผ ๋‘๊ณ  ํ’€์—ˆ๋‹ค

์„ธ๊ฐœ ์กฐํ•ฉ์„ ๊ตฌํ•ด์•ผํ•˜๋‹ˆ ํฌ์ธํ„ฐ๋ฅผ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

ํฌ์ธํ„ฐ ํ•œ๊ฐœ๋ฅผ ๊ณ ์ •์‹œํ‚ค๊ณ  ๊ธฐ์กด ๋ฌธ์ œ ํ’€๋“ฏ์ด ํˆฌํฌ์ธํ„ฐ ๋กœ์ง์œผ๋กœ ์กฐํ•ฉ์„ ๊ตฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๊ณ ์ •์‹œํ‚จ ํฌ์ธํ„ฐ๋Š” for๋ฌธ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋Œ๋ ค๋ฒ„๋ฆฌ์ž

 

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

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

using namespace std;

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

int main() {
    int n;
    cin >> n;
    vector<long long>v(n);
    for(int i=0; i<n; i++) {
        cin >> v[i];
    }
    
    sort(v.begin(), v.end());
    
    int fixed=0, left=1, right=n-1;
    tuple<int, int, int> ans = {fixed, left, right};
    long long cur = getAbs(v[fixed] + v[left] + v[right]); 
    
    for(int i=0; i<n-2; i++) {
        fixed = i;
        left = i+1;
        right = n-1;
        
        while(left<right) {
            long long sum = v[fixed] + v[left] + v[right];

            if(getAbs(sum) < cur) {
                cur = getAbs(sum);
                ans = {fixed, left, right};
            }
        
            if(sum<0) {
                left++;
            } else {
                right--;
            }
        } 
    }
    
    cout << v[get<0>(ans)] << " " << v[get<1>(ans)] << " " << v[get<2>(ans)];
    
    return 0;
}
๋ฐ˜์‘ํ˜•