๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] 2467 ์šฉ์•ก ์ด๋ถ„ํƒ์ƒ‰

by Jouureee 2021. 2. 5.

๋ฌธ์ œ :

www.acmicpc.net/problem/2467

 

2467๋ฒˆ: ์šฉ์•ก

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

www.acmicpc.net

๋ถ„์„ : 

์ด ๋ฐฉ์‹์€ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค. ์‚ฐ์„ฑ๊ณผ ์•Œ์นผ๋ฆฌ ์šฉ์•ก์„ ๊ตฌ๋ถ„ํ•ด ๋ฐฐ์—ด์— ๋‹ด์•˜๊ณ  ๊ฐ ๋ฐฐ์—ด์ด ๋๋‚ ๋•Œ๊นŒ์ง€ O(n^2)์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ๊ฐ€์žฅ ๋น„ํšจ์œจ์ ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ‘ผ๊ฑฐ ๊ฐ™๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋”ฑํžˆ map ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋˜์—ˆ๋Š”๋ฐ ์“ธ๋ฐ ์—†๋Š” ์•…์„ธ์‚ฌ๋ฆฌ๊ฐ€ ๋งŽ์€ ๋Š๋‚Œ .. 

<๋‘์šฉ์•ก> ๋ฌธ์ œ์—์„œ ๋” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. 

C++ ์ฝ”๋“œ :

//
//  2467_solution.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/02/05.
//

#include <stdio.h>
#include <iostream>
#include <unordered_map>
#include <math.h>
#include <string>

using namespace std;

unordered_map<string, int> solution(int arr[],int n){
    int acidArr[n];
    int alkaliArr[n];
    fill_n(acidArr, n, -1);
    fill_n(alkaliArr, n, -1);
    
    int acidCount = 0;
    int alkaliCount = 0;
    
    unordered_map<string, int> mapping;
    mapping["acid"] = 0;
    mapping["alkali"] = 0;
    
    for(int i =0; i<n; i++){
        if(arr[i] < 0){ //put in acidArr
            acidArr[acidCount] = arr[i];
            acidCount++;
        }else if (arr[i] > 0){
            alkaliArr[alkaliCount] = arr[i];
            alkaliCount++;
        }
    }

    int initDiff = 0xffff;
    for(int acidJ = 0; acidArr[acidJ] != -1; acidJ++){
        for(int alkaliJ = 0; alkaliArr[alkaliJ] != -1; alkaliJ++){
            int diff = (acidArr[acidJ] + alkaliArr[alkaliJ]) - 0;
            if(abs(diff) < abs(initDiff)){
                initDiff = diff;
                mapping["acid"] = acidArr[acidJ];
                mapping["alkali"] = alkaliArr[alkaliJ];
            }
        }
    }
    
    return mapping;
}


๋Œ“๊ธ€