๐Ÿ’ญ Problem Solving/C++

[SWEA][C++] 1208. [S/W ๋ฌธ์ œํ•ด๊ฒฐ ๊ธฐ๋ณธ] 1์ผ์ฐจ - Flatten

0=2. 2024. 4. 27. 20:19

๋ฌธ์ œ

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV139KOaABgCFAYh

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

ํ’€์ด

์ฃผ์–ด์ง„ ํšŸ์ˆ˜๋งŒํผ ์ตœ๊ณ ์ ๊ณผ ์ตœ์ €์ ์˜ ์ฐจ์ด๋ฅผ ์ค„์—ฌ๋‚˜๊ฐ„๋‹ค. ์ •๋ ฌ์„ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

1. ์ƒ์ž์˜ ๋†’์ด๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
2. ํ‰ํƒ„ํ™” ๋ฐ˜๋ณต
    (1) ์ƒ์ž์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ(์ตœ์ €์ ) +1, ๋งˆ์ง€๋ง‰ ์›์†Œ(์ตœ๊ณ ์ ) -1
    (2) ์ƒ์ž๋ฅผ ์žฌ์ •๋ ฌ
    (3) ์ฃผ์–ด์ง„ ํšŸ์ˆ˜๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ–ˆ๊ฑฐ๋‚˜, ์ตœ๊ณ ์ ๊ณผ ์ตœ์ €์ ์˜ ์ฐจ์ด๊ฐ€ 1 ์ดํ•˜๊ฐ€ ๋˜๋ฉด ๋ฐ˜๋ณต ์ข…๋ฃŒ
3. ์ตœ๊ณ ์ ๊ณผ ์ตœ์ €์ ์˜ ๋†’์ด ์ฐจ ๋ฐ˜ํ™˜

์ฝ”๋“œ

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

using namespace std;

int main() {
    int test_case;
    int T = 10;
    for (test_case = 1; test_case <= T; ++test_case) {
        int cnt;
        vector<int> v(100, 0);

        // ์ž…๋ ฅ
        cin >> cnt;
        for (int &i: v) {
            cin >> i;
        }

        // ์—ฐ์‚ฐ
        sort(v.begin(), v.end());

        while (cnt--) {
            v.front()++;
            v.back()--;

            sort(v.begin(), v.end());

            if (v.back() - v.front() <= 1) {
                break;
            }
        }

        // ์ถœ๋ ฅ
        cout << '#' << test_case << ' ' << v[v.size() - 1] - v[0] << '\n';
    }
    return 0;
}