J-Data Time? — 2025 Nowcoder Winter Algorithm Basic Training Camp 2 Solutions
Last updated on January 24, 2025 am
This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to original Chinese version. I am continuously working to improve the translation.
Link: https://ac.nowcoder.com/acm/contest/95334/J Source: Nowcoder
Problem Description
Niukele is analyzing the situation of users logging into the Nowcoder APP over the past few months. The data he has collected consists of three fields: user_id representing the user ID, login_date representing the login date, and login_time representing the login time. He would like you to help him count how many people logged into the APP during each of the three time periods — commute, lunch break, and bedtime — in month m of year h. In particular, multiple logins by the same person within the same time period are counted as one.
In this problem, the commute time periods are 07:00:00-09:00:00 and 18:00:00-20:00:00; the lunch break time period is 11:00:00-13:00:00; and the bedtime time period is 22:00:00-01:00:00. All time periods include both boundary values.
Input Description:
The first line contains three integers n,h,m (1≦n≤105; 2000≦h≦2030; 1≦m≦12) representing the total number of data records you need to process, the year to query, and the month to query. The following n lines each first contain an integer user_id (1≦user_id≦10^20) representing the user ID; then a date login_date in YYYY-MM-DD format representing the user's login date; and finally a time login_time in hh:mm:ss format representing the user's login time.
A time in YYYY-MM-DD format is defined as: 2000≦YYYY≦2030 represents the year, 1≦MM≦12 represents the month, and 1≦DD≦31 represents the day. Dates follow the common year/leap year rules. A time in hh:mm:ss format is defined as: 0≦hh≦23 represents the hour, 0≦mm≦59 represents the minute, and 0≦ss≦59 represents the second.
Output Description:
Output three integers on one line representing the number of logins during the commute, lunch break, and bedtime periods. Note that multiple logins by the same person within the same time period are counted as one.
Input
13 2021 12
2 2021-11-08 20:00:00
2 2021-12-03 07:00:00
3 2021-12-03 11:00:00
3 2021-12-05 12:00:00
4 2024-11-08 20:00:00
2 2021-12-06 09:20:00
2 2021-12-06 18:20:00
2 2021-12-07 13:00:00
2 2021-12-31 22:00:00
3 2021-12-30 21:00:00
4 2021-12-30 00:50:00
4 2021-12-31 00:30:00
2 2021-11-09 17:00:00Output
1 2 2 Example 2
Input
2 2025 1
1000 2024-12-31 23:59:59
1000 2025-01-01 00:00:00Output
0 0 1Explanation
First check the year and month, then check the time.
Example 3
Input
2 2025 1
1 2025-01-23 00:00:00
1 2025-01-22 00:00:00Output
0 0 1Explanation
The same time period is not counted more than once.
Solutions
This problem is actually not difficult — it is a simulation, but you can flexibly use various STL containers to make the implementation very simple.
First, to count each person's logins, we don't need to record all information for everyone and then make judgments; we only need to finish the judgment within a single read in the loop.
Second, the requirement in the problem statement that "multiple logins by the same person within the same time period are counted as one" can be understood as automatic deduplication, so it is not hard to think of the unordered_set container, which automatically deduplicates.
So we define three containers for the three time periods, storing the people who logged into the APP during each of the three periods.
That is, insert the account each time, and finally output size().
Next are the times themselves.
First, for the year and month, we can read in login_date and then use the sscanf function to compare with the h and m read in at the beginning; if the check fails, we continue.
However, we read in using string, while the sscanf function only supports char[] — what should we do?
We can use a member function of the std::string class, .c_str(), which converts a std::string object into a C-style string (i.e., a character array terminated by \0).
After that comes the issue of login_time. We notice that since the formats are all the same, we can directly use lexicographic comparison to determine whether the time falls within the range we need.
That is, we can define a function like this
bool P(string time,string start,string end){
return time>=start&&time<=end;
}It returns true if the time falls within the range we need.
Next, if we just brute-force it like this: if(P(login_time,"07:00:00","09:00:00")||P(login_time,"18:00:00","20:00:00")), that would of course also work, but we can make a better choice.
We notice that a time period consists of two parts: a start and an end, which naturally leads us to consider using a pair to store a time period. Since the commute time and bedtime time each consist of two time periods, we can use a vector to store the pairs, and then use a range-based loop to solve the problem.
// 定义时间段
vector<pair<string, string>> rushHours = {{"07:00:00", "09:00:00"}, {"18:00:00", "20:00:00"}};
vector<pair<string, string>> lunchHours = {{"11:00:00", "13:00:00"}};
vector<pair<string, string>> sleepHours = {{"22:00:00", "23:59:59"}, {"00:00:00", "01:00:00"}}; //记得拆分The final code implementation is as follows
代码
#include<bits/stdc++.h>
#include <unordered_set>
using namespace std;
const int maxn=1e5+10;
int n,h,m;
string user_id,login_date,login_time;
unordered_set<string> tongqing,wuxiu,lingshui;
bool P(string time,string s,string e){
return time>=s&&time<=e;
}
// 定义时间段
vector<pair<string, string>> rushHours = {{"07:00:00", "09:00:00"}, {"18:00:00", "20:00:00"}};
vector<pair<string, string>> lunchHours = {{"11:00:00", "13:00:00"}};
vector<pair<string, string>> sleepHours = {{"22:00:00", "23:59:59"}, {"00:00:00", "01:00:00"}};
int main(){
cin>>n>>h>>m;
for(int i=0;i<n;i++){
cin>>user_id>>login_date>>login_time;
int year,month;
sscanf(login_date.c_str(),"%d-%d",&year,&month);
if(year!=h||month!=m) continue;
for(const auto& x:rushHours){
if(P(login_time,x.first,x.second)){
tongqing.insert(user_id);
break;
}
}
for(const auto& x:lunchHours){
if(P(login_time,x.first,x.second)){
wuxiu.insert(user_id);
break;
}
}
for(const auto& x:sleepHours){
if(P(login_time,x.first,x.second)){
lingshui.insert(user_id);
break;
}
}
}
cout<<tongqing.size()<<' '<<wuxiu.size()<<' '<<lingshui.size()<<endl;
}