출처: https://cses.fi/problemset/task/1163


문제 설명

There is a street of length x whose positions are numbered 0,1,,x. Initially, there are no traffic lights, but n sets of traffic lights are added to the street one after another.

Your task is to calculate the length of the longest passage without traffic lights after each addition.

입력 양식

The first input line contains two integers x and n: the length of the street and the number of sets of traffic lights.

Then, the next line contains n integers p_1,p_2,,p_n : the position of each set of traffic lights. Each position is distinct.

입력 예

8 3
3 6 2

출력 양식

Print the length of the longest passage without traffic lights after each addition.

출력 예

5 3 3

제약조건

  • 1 x 10^9
  • 1 n 2x10^^5
  • 0 < p_i < x

문제 풀이 내용

입력받은 가로등 위치를 set에 입력하고, 원래 있던 가로등 위치에서 가장 가까운 가로등 위치를 찾아서 간격을 계산한다. 

프로그램 내용

더보기
...
    while(nLight--)
    {
        long temp;
        cin >> temp;

        auto it = street.lower_bound(make_tuple(temp,0));
        int pre, cur;
        tie(pre, cur) = *it;

        street.erase(it);
        street.insert(make_tuple(temp, cur-(pre-temp)));
        street.insert(make_tuple(pre, pre-temp));

        t_street.erase(make_tuple(cur,pre));
        t_street.insert(make_tuple(cur-(pre-temp), temp));
        t_street.insert(make_tuple(pre-temp, pre));

        cout << get<0>(*t_street.rbegin());
        if ( nLight > 0 ) cout << " " ;
    }
...

 

Sorting and Searching link )

'CSES' 카테고리의 다른 글

CSES 2. Factory Machines (1620)  (0) 2019.10.01
CSES 2. Room Allocation (1164)  (0) 2019.10.01
CSES 2. Tasks and Deadlines (1630)  (0) 2019.09.28
CSES 2. Towers (1073)  (0) 2019.09.28
CSES 2. Playlist (1141)  (0) 2019.09.28

+ Recent posts