BOJ : https://www.acmicpc.net/problem/15922

2170번 선긋기와 같은 문제입니다.

 

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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
typedef pair<intint> pii;
int N;
 
vector<pii> v;
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
 
    cin >> N;
    for (int i = 0; i < N; i++) {
        int s, e;
        cin >> s >> e;
        v.push_back({ s,e });
    }
 
    sort(v.begin(), v.end());
 
    int s = v[0].first, e = v[0].second;
    int ans = 0;
    for (int i = 0; i < N; i++) {
        int ns = v[i].first, ne = v[i].second;
        if (e < ns) {
            ans += (e - s);
            s = ns, e = ne;
        }
        else e = max(e, ne);
 
    }
    ans += e - s;
 
    cout << ans;
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 1062번 가르침  (0) 2020.02.05
BOJ 1072번 게임  (0) 2020.02.05
BOJ 2170번 선 긋기  (0) 2020.02.05
BOJ 9202번 Boggle  (0) 2020.02.05
BOJ 10999번 구간 합 구하기2  (0) 2020.02.05

+ Recent posts