본문 바로가기

Baekjoon(C++)

[C++]백준 알고리즘 2246번

풀이는 아래와 같다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
 
using namespace std;
 
int main(){
    
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    int n;
    cin >> n;
    //allocation
    int** condo = new int*[n];
    for(int i=0; i<n; i++){
        condo[i] = new int[2];
    }
    
    for(int i=0; i<n; i++){
        cin >> condo[i][0>> condo [i][1] ;
    }
    
    int count = 0;
    bool break_out = false;
    for(int i=0; i<n; i++){
        break_out = false;
        for(int j=0; j<n; j++){
            // if(j == i) continue;
            if(condo[j][1< condo[i][1&& condo[j][0<= condo[i][0]){
                break_out = true;
                break;
            }if(condo[j][1<= condo[i][1&& condo[j][0< condo[i][0]){
                break_out = true;
                break;
            }
        }
        if(break_out == true){
            continue;
        }else{
            count++;
        }
    }
    
    cout << count << '\n';
    
    //deallocation
    for(int i=0; i<n; i++){
        delete[] condo[i];
    }
    delete[] condo;
    return 0;
    
}
 
 
cs

시행착오:

처음에는 알고리즘 자체를 조금 잘못 생각했었다. 문제의 두 조건은 서로 대우 명제가 아닌가..? 그래서 1번이나 2번이나 같은 뜻이 아닌가 생각했었는데 다시 생각해보니 함정이 있었다. 만약 거리나 금액이 같은 경우가 있다면?!?! 이를 고려하여 문제를 다시 풀어보니 정답이 제대로 나왔다.

반응형

'Baekjoon(C++)' 카테고리의 다른 글

[C++]백준 알고리즘 19532번  (0) 2021.07.11
[C++]백준 알고리즘 1463번  (0) 2021.07.11
[C++]백준 알고리즘 10871번  (0) 2021.07.03
[C++]백준 알고리즘 2439번  (0) 2021.07.03
[C++]백준 알고리즘 2438번  (0) 2021.07.03