ZeroJudge N630: 電影院 (Cinema)

You suddenly want to watch a movie, so you search online for the movie schedule at nearby theaters. Not wanting to wait too long, you decide to find the earliest movie showing. However, considering you still need to get to the theater, buy tickets, and get popcorn, you need to reserve at least 20 minutes (starting from now). Please write a program that, given the current time and a series of movie start times (sorted), finds the earliest movie time you can attend. Movie times are represented in a 24-hour format.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line consists of a positive integer N (1 ≤ N ≤ 1000), indicating the number of movies in the movie schedule.
Following that are N lines, each representing the start time of a movie, formatted as HH MM (0 ≤ HH < 24, 1 ≤ MM < 60).
The N+2 line represents the current time, formatted as HH MM (0 ≤ HH < 24, 1 ≤ MM < 60).
The output should be the time in HH MM format, representing the earliest movie time you can attend. If there are no movies to watch, the output should indicate so.
If all movie start times are earlier than 20 minutes from the current time, output "Too Late".
3
14 50
15 05
16 00
14 30
14 50
2
23 15
23 20
23 00
23 20
1
09 00
10 00
Too Late

Thought Process

Convert all times into minutes and add 20 minutes to the current time. Then, start from the beginning and check if the time is greater than or equal to the current time. If so, output that time.

If integers are used to collect the data, prepend a "0" before outputting single-digit numbers.

Sample Code-ZeroJudge N630: 電影院 (Cinema)

#include <iostream>
#include <vector>
using namespace std;

string output(int N)
{
    if (N < 10) return "0" + to_string(N);
    return to_string(N);
}

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N;
    vector<pair<int, int>>time;
    for (int i = 0; i<N; i++)
    {
        int H, M;
        cin >> H >> M;
        pair<int, int>T;
        T.first = H;
        T.second = M;
        time.push_back(T);
    }
    int H, M, T;
    cin >> H >> M;
    T = (H*60) + M + 20;
    bool ok = false;
    for (int i = 0; i<N; i++)
    {
        int calc = (time[i].first * 60) + time[i].second;
        if (calc >= T)
        {
            cout << output(time[i].first) << " " << output(time[i].second) << "\n";
            ok = true;
            break;
        }
    }
    if (!ok) cout << "Too Late\n";
}

//ZeroJudge N630
//Dr. SeanXD

Comments