๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฐฑ์ค€

boj >> 1931 ํšŒ์˜์‹ค ๋ฐฐ์ •(c++)

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
// 200412 1931.ํšŒ์˜์‹ค ๋ฐฐ์ •
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long 
using namespace std;
 
bool ncompare(const pair<ll, ll> &p1, const pair<ll, ll> &p2){
    if(p1.second == p2.second) return p1.first < p2.first;
    else return p1.second < p2.second;
    // return๊ฐ’์ด true์ด๋ฉด ์™ผ์ชฝ ๊ฐ’์ด ์˜ค๋ฅธ์ชฝ ๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ  ์ •๋ ฌ์„ ์ด๋Ÿฐ์‹์œผ๋กœ ํ•ด์ค€๋‹ค.
}
int main(){
    int N, b, e; 
    int count = 0end = 0;
    //์‹œ๊ฐ„์˜ ๋ฒ”์œ„๊ฐ€ 2^31-1๊นŒ์ง€์ž„.
    cin >> N; 
    vector<pair<ll, ll> > V;
 
    for(int i=0; i<N; i++){
        cin >> b >> e;
        V.push_back(make_pair(b, e));
    }
    sort(V.begin(), V.end(), ncompare);
 
    for(int i=0; i<V.size(); i++){
        // * ํšŒ์˜๊ฐ€ ์ผ์ฐ ๋๋‚˜๋Š” ๊ธฐ์ค€์„ ์žก์ž * 
        if(V[i].first >= end) {
            count++;
            end = V[i].second;
        }
    }
    cout << count << endl;
}
cs

 

 

'์•Œ๊ณ ๋ฆฌ์ฆ˜ > ๋ฐฑ์ค€' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

boj >> 2193 ์ด์นœ์ˆ˜  (0) 2020.03.29
boj >> 17070 ํŒŒ์ดํ”„ ์˜ฎ๊ธฐ๊ธฐ1  (0) 2020.03.28