OSgood의 개발일기

[백준] 8984 막대기 본문

Algorithm/Algorithm 문제 연습

[백준] 8984 막대기

OSgood 2020. 3. 22. 01:11

 

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
 
using namespace std;
 
using ll = long long;
 
 
struct UpDown
{
    ll up;
    ll down;
 
    bool operator<(UpDown x)
    {
        if (up == x.up)
        {
            return down < x.down;
        }
        return up < x.up;
    }
};
int n;
int l;
unordered_map <intint> t_hash, d_hash;
int main()
{
    scanf("%d %d"&n, &l);
    vector < UpDown > stick(n);
    vector <int> t_point(n), d_point(n);
    
    for (int i = 0; i < n; i++)
    {
        scanf("%lld %lld"&stick[i].up, &stick[i].down);
        t_point[i] = stick[i].up;
        d_point[i] = stick[i].down;
    }
 
    sort(stick.begin(), stick.end());
    sort(t_point.begin(), t_point.end());
    sort(d_point.begin(), d_point.end());
 
    t_point.erase(unique(t_point.begin(), t_point.end()), t_point.end());
    d_point.erase(unique(d_point.begin(), d_point.end()), d_point.end());
 
    vector <ll> t_ans(t_point.size());
    for (int i = 0; i < t_point.size(); i++)
        t_hash[t_point[i]] = i;
 
    vector <ll> d_ans(d_point.size());
    for (int i = 0; i < d_point.size(); i++)
        d_hash[d_point[i]] = i;
 
    ll distance = 0;
    ll temp_t = 0;
    ll result = 0;
    int  index_t, index_d;
    for (int i = 0; i < n; i++)
    {
        distance = abs(stick[i].up - stick[i].down) + l;
        index_t = t_hash[stick[i].up];
        index_d = d_hash[stick[i].down];
        temp_t = t_ans[index_t];
        t_ans[index_t] = max(t_ans[index_t], d_ans[index_d] + distance);
        d_ans[index_d] = max(d_ans[index_d], temp_t + distance);
        result = max(result, max(t_ans[index_t], d_ans[index_d]));
    }
 
    cout << result;
    return 0;
}
cs

'Algorithm > Algorithm 문제 연습' 카테고리의 다른 글

[백준] 2517 달리기  (0) 2020.03.29
[백준] 8986 전봇대  (0) 2020.03.22
[백준] 8983번 사냥꾼  (0) 2020.03.21
[백준] 10165 버스 노선  (0) 2020.03.14
[백준] 10166번 관중석  (0) 2020.03.13
Comments