๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 11497๋ฒˆ - ํ†ต๋‚˜๋ฌด ๊ฑด๋„ˆ๋›ฐ๊ธฐ

by dkswnkk 2021. 11. 5.
 

11497๋ฒˆ: ํ†ต๋‚˜๋ฌด ๊ฑด๋„ˆ๋›ฐ๊ธฐ

๋‚จ๊ทœ๋Š” ํ†ต๋‚˜๋ฌด๋ฅผ ์„ธ์›Œ ๋†“๊ณ  ๊ฑด๋„ˆ๋›ฐ๊ธฐ๋ฅผ ์ข‹์•„ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ N๊ฐœ์˜ ํ†ต๋‚˜๋ฌด๋ฅผ ์›ํ˜•์œผ๋กœ ์„ธ์›Œ ๋†“๊ณ  ๋›ฐ์–ด๋†€๋ ค๊ณ  ํ•œ๋‹ค. ๋‚จ๊ทœ๋Š” ์›ํ˜•์œผ๋กœ ์ธ์ ‘ํ•œ ์˜† ํ†ต๋‚˜๋ฌด๋กœ ๊ฑด๋„ˆ๋›ฐ๋Š”๋ฐ, ์ด๋•Œ ๊ฐ ์ธ์ ‘ํ•œ ํ†ต๋‚˜๋ฌด์˜ ๋†’์ด

www.acmicpc.net

 ์ •๊ทœ๋ถ„ํฌํ‘œ์ฒ˜๋Ÿผ ๋ฐฐ์—ด ๊ฐ€์šด๋ฐ ์ œ์ผ ํฐ ๊ฐ’์„ ๋„ฃ๊ณ  ์–‘์ชฝ์— ์ ์  ์ž‘์•„์ง€๋Š” ์ˆ˜๋กœ ๋‚˜์—ด์‹œํ‚จ๋‹ค.
 ๊ทธ ํ›„ ์„ ํ˜•ํƒ์ƒ‰์„ ํ•ด์„œ ์ฐจ์ด๋ฅผ ๊ตฌํ•ด ์ฐจ์ด๊ฐ€ ์ ค ํฐ  ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

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

using namespace std;


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int T; cin>>T;

    while(T--){
        int n,ans=0; cin>>n;
        vector<int>v;
        for(int i=0; i<n; i++){
            int inp; cin>>inp;
            v.push_back(inp);
        }
        sort(v.begin(),v.end());


        deque<int> dq;
        int max_value;

        for(int i=v.size()-1; i>=0; i--){
            max_value = v[i];
            if(i&1) dq.push_back(max_value);
            else dq.push_front(max_value);
        }


        ans=max(abs(dq[0]-dq[dq.size()-1]),abs(dq[dq.size()-1]-dq[dq.size()-2]));

        for(int i=1; i<dq.size()-1; i++){
            ans=max(abs(dq[i]-dq[i-1]),max(abs(dq[i]-dq[i+1]),ans));
        }
        cout<<ans<<'\n';

    }
}

๋Œ“๊ธ€