백준 문제풀이/Bruteforcing

[C++] 백준 문제풀이 (Bruteforcing) 1059번 좋은 구간

코딩준우 2023. 6. 27. 21:46

 

 

 

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

 

1059번: 좋은 구간

[9, 10], [9, 11], [9, 12], [10, 11], [10, 12]

www.acmicpc.net

 

 

//[C++] 백준 문제풀이 (Bruteforcing)

#include <bits/stdc++.h>

int L;
int n;
int S[51];

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

    S[0] = 0;
    std::cin >> L; 
    for (int i = 1; i <= L; ++i) {
        std::cin >> S[i];
    }

    std::sort(S, S + L + 1);

    std::cin >> n;
    bool flag = true;
    int start;
    int end;

    for (int i = 1; i <= L; ++i){
        if (S[i] == n) 
            flag = false;
        else if (S[i] > n){
            start = S[i - 1] + 1;
            end = S[i] - 1;
            break;
        }
    }

    if (flag){
        /*
        start      n      end
        start ~ n 까지의 개수 (n - start + 1) 중에 1개 선택
        n ~ end 까지의 개수 (end - n + 1) 중에 1개 선택
        그 중에 (n, n)을 선택한 경우를 뺀다.
        (n - start + 1) * (end - n + 1) - 1 
        */
        std::cout << (n - start + 1) * (end - n + 1) - 1 << "\n";
    }
    else {
        std::cout << 0 << "\n";
    }
    return 0;
}