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


문제 설명

In a movie festival n movies will be shown. You know the starting and ending time of each movie. What is the maximum number of movies you can watch entirely?

입력  

The first input line has an integer n: the number of movies.

After this, there are n lines that describe the movies. Each line has two integers aa and bb: the starting and ending times of a movie.

출력

Print one integer: the maximum number of movies.

입력 예

3
3 5
4 9
5 8

출력 예

2

제약조건

  • 1 <= n <= 2x10^5
  • 1 <= a < b <= 10^9

문제 풀이 내용

영화가 끝나는 시간을 기준으로 정렬한다.

영화 끝나는 시간이 다음 영화 시작 시간 전에 끝나면, 볼 수 있는 영화편수를 증가시키고, 영화 끝나는 시간을 다음 영화 시간으로 갱신한다.  

프로그램 내용

더보기
...
    vector <pair<int, int>> m_time;

    for(int idx=0; idx < n_movie; ++idx)
        int start_time, end_time; 
        m_time.push_back({end_time, start_time});

    sort(m_time.begin(), m_time.end()); /// sort by end_time 

    int c_movie=0;
    int time_now=0;
    for (int i = 0; i< n_movie; ++i)
        /// start_time > time_now 
        /// time_now = end_time, increase counter
        if (m_time[i].second >= time_now)
            time_now = m_time[i].first;
            c_movie++;
...

 

Sorting and Searching link )

'CSES' 카테고리의 다른 글

CSES 2. Maximum Subarray Sum (1643)  (0) 2019.09.26
CSES 2. Sum of Two Values (1640)  (0) 2019.09.26
CSES 2. Restaurant Customers (1619)  (0) 2019.09.25
CSES 2. Concert Tickets (1091)  (0) 2019.09.25
CSES 2. Ferris Wheel (1090)  (0) 2019.09.25

+ Recent posts