๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/BOJ

[C++/BOJ] 2467 : ์šฉ์•ก (ํˆฌํฌ์ธํ„ฐ)

728x90

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์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์šฉ์•ก์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋‘ ๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ์—๋Š” ๊ทธ ์ค‘ ์•„๋ฌด๊ฒƒ์ด๋‚˜ ํ•˜๋‚˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


 

๊ณจ๋“œ5 ๋ฌธ์ œ.

์ „ํ˜•์ ์ธ ํˆฌํฌ์ธํ„ฐ ๋ฌธ์ œ์ด๋‹ค!

์ฒ˜์Œ์— nowsum์„ ๊ตฌํ• ๋•Œ abs๋ฅผ ์ ์šฉํ•ด๋ฒ„๋ฆฌ๋Š” ๋ฐ”๋žŒ์—, ํฌ์ธํ„ฐ๊ฐ€ ์ œ๋Œ€๋กœ ์•ˆ ์›€์ง์˜€์Œ..

์–ธ์ œ ์ ˆ๋Œ“๊ฐ’์„ ์“ฐ๊ณ , ์–ธ์ œ ์•ˆ ์“ฐ๋Š”์ง€ ์ž˜ ํŒŒ์•…ํ•ด์„œ ๋„ฃ์–ด์ค˜์•ผํ•œ๋‹ค.

 

 

 

๋‚˜์˜ ํ’€์ด

#include <iostream>
using namespace std;

int main(){
    int n;
    int num[100001] = {0};
    cin >> n;
    for (int i = 0; i < n; ++i){
        cin >> num[i];
    }

    int left = 0;
    int right = n - 1;
    int minsum = abs(num[left] + num[right]);
    int minleft = num[left];
    int minright = num[right];

    for (int i = 0; i < n; ++i){
        
        int nowsum = num[left] + num[right];
        if (minsum > abs(nowsum)){
            minsum = abs(nowsum);
            minleft = num[left];
            minright = num[right];
            if(minsum==0) break;
        }
        if(nowsum > 0) right--;
        if(nowsum < 0) left++;
        if(right == left) break;
    }
    cout << minleft << " " << minright;
}

 

728x90