解题思路:
构造时间结构体的思路很容易想到,成员包括年份、月份、日期、时、分和秒。重构一些比较、赋值函数。用三个数组分别存储每个人的任务的开始时间,结束时间和所有的时间节点(包括总的开始时间和结束时间)。这个记录所有的时间节点的数组t非常重要,用来作为寻找公共时间的载体。我们把这些时间节点排序后想象成在一个一维坐标线上,我们从前向后遍历寻找符合要求的最大时间段,而每一个时间段由左端点l和右端点r组成,右端点r需要尽量向右,左端点l需要尽量向左。因此还需要用到判断左右端点是否空闲的函数以及判断时间段是否长度达到1小时的函数。左右时间端点从坐标线最左端开始向右遍历,直到到达右端点,每次找到符合条件的时间段则输出,若最后符合条件的时间段个数为0,则输出"no appointment possible"。注意有多组数据,因此每次需要对变量、数组初始化以及输出格式的问题。
总结:
一道有难度的题目,构造time结构体很容易想到,但是寻找符合条件的时间段不太容易,需要想到类似于滑动窗口一样应用两个时间段的左右端点从前向后来遍历搜寻符合要求的最长的时间段。
参考代码:
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
struct time{
int year,month,day,hour,minute,second;
time(int y=0,int m=0,int d=0,int h=0,int mi=0,int s=0):year(y),month(m),day(d),hour(h),minute(mi),second(s){
}
bool operator < (const time &t)const
{
if (year != t.year)return year < t.year;
if (month != t.month)return month < t.month;
if (day != t.day)return day < t.day;
if (hour != t.hour)return hour < t.hour;
if (minute != t.minute)return minute < t.minute;
return second < t.second;
}
bool operator > (const time &t)const
{
if (year != t.year)return year > t.year;
if (month != t.month)return month > t.month;
if (day != t.day)return day > t.day;
if (hour != t.hour)return hour > t.hour;
if (minute != t.minute)return minute > t.minute;
return second > t.second;
}
bool operator <=(const time &t)const
{
if (year != t.year)return year <= t.year;
if (month != t.month)return month <= t.month;
if (day != t.day)return day <= t.day;
if (hour != t.