解题思路:
其实题目并不难,但有很有易错点。其实时间可以转换成整数,难度会减少许多,或者像我一样定义结构体,但还要重载很多函数。把每个时间想象成分布在一个圆环上面,因为活动是循环进行的,就在圆环上遍历即可,不需要考虑天与天之间的界限。然后先把所有的需要醒着的时间段放在awake数组里,然后检验那些其他时间段能睡觉吗,能的话放在sleep数组里,不能的话检验能否和相邻时间段合并,不能的话直接无解,可以的话进行合并。注意这里可以真的合并,但容易出错,推荐用一个last表示上一个醒着的时间段会简单很多。最后输出sleep里的时间段即可。如果sleep为0也无解,猫不能一直不睡觉。
参考代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <utility>
#include <vector>
using namespace std;
int A,B,n;
struct interval{//时间段结构体
int start_hour,start_min,end_hour,end_min;
int time;//minutes为单位
interval(int a=0,int b=0,int c=0,int d=0,int t=0){
start_hour=a;
start_min=b;
end_hour=c;
end_min=d;
time=t;
}
bool operator < (const interval &y)const {
if(start_hour!=y.start_hour)return start_hour<y.start_hour;
return start_min<y.start_min;
}
};
int get_time(int a,int b,int c,int d){//求a:b到c:d的时长(minutes)
if((a<c)||(a==c&&b<=d)){
return (c-a)*60+d-b+1;
}
else{
c+=24;
return (c-a)*60+d-b+1;
}
}
vector<interval> sleep;//记录睡觉的时间段
vector<interval> awake;//记录醒着的时间段
void get_next_time(int x,int y,int &a,int &b){//求x:y的下一分钟并赋值给a:b
if(y!=59)y++;
else{
y=0;
if(x!=23)x++;
else x=0;
}
a=x;b=y;
}
void get_back_time(int x,int y,int &a,int &b){//求x:y的上一分钟并赋值给a:b
if(y!=0)y--;
else{
y=59;
if(x!=0)x--;
else x=23;
}
a=x;b=y;
}
bool judge;//true:有解 false:无解
interval last;//记录上一个醒着的时间段
int main(){
while (cin>>A>>B>>n) {
judge=true;
sleep.clear();
awake.clear();
int tn=n;//记录一下n
while (n--) {
int a,b,c,d;
char ch;
scanf("%d %c %d %c %d %c %d",&a,&ch,&b,&ch,&c,&ch,&d);
int time=get_time(a, b, c, d);
if(time>60*B){
judge=false;//电视剧时长超过了B
}
awake.push_back(interval(a, b, c, d,time));//把这段电视剧时间加入醒着的时间
}
if(!judge)cout<<"No\n";
else{
sort(awake.begin(), awake.end());//从小到大排序
//初始awake[0]为上一个醒着的时间段
last.start_hour=awake[0].start_hour;
last.start_min=awake[0].start_min;
last.end_hour=awake[0].end_hour;
last.end_min=awake[0].end_min;
for(int i=1;i<tn;i++)
{
int a,b,c,d;//a:b-c:d为两个醒着的时间中间那段时间
get_next_time(last.end_hour, last.end_min, a, b);
get_back_time(awake[i].start_hour, awake[i].start_min, c, d);
int time=get_time(a, b, c, d);
if(time>=60*A&&time<24*60){//这段时间可以睡觉
if(a!=b||c!=d)
sleep.push_back(interval(a,b,c,d,time));
last.start_hour=awake[i].start_hour;
last.start_min=awake[i].start_min;
last.end_hour=awake[i].end_hour;
last.end_min=awake[i].end_min;
}
else{//这段时间不可以睡觉,合并相邻两个醒着的时间为一个新的醒着的时间
last.end_hour=awake[i].end_hour;
last.end_min=awake[i].end_min;
int new_a=last.start_hour;
int new_b=last.start_min;
int new_c=last.end_hour;
int new_d=last.end_min;
int new_time=get_time(new_a, new_b, new_c, new_d);
if(new_time>60*B){judge=false;break;}//醒着的时间超时,无解
}
}
//单独考虑最后一个时间段
int a,b,c,d;//a:b-c:d为两个醒着的时间中间那段时间
get_next_time(last.end_hour, last.end_min, a, b);
get_back_time(awake[0].start_hour, awake[0].start_min, c, d);
int time=get_time(a, b, c, d);
if(time>=60*A&&time<24*60){//这段时间可以睡觉
if(a!=b||c!=d)
sleep.push_back(interval(a,b,c,d,time));
}
else{//这段时间不可以睡觉,合并相邻两个醒着的时间为一个新的醒着的时间
int new_a=last.start_hour;
int new_b=last.start_min;
int new_c=awake[0].end_hour;
int new_d=awake[0].end_min;
int new_time=get_time(new_a, new_b, new_c, new_d);
if(new_time>60*B)judge=false;//醒着的时间超时,无解
}
if(!judge||sleep.size()==0)cout<<"No\n";
else{
cout<<"Yes\n";
cout<<sleep.size()<<endl;
for (int i=0; i<sleep.size(); i++)
printf("%02d:%02d-%02d:%02d\n",sleep[i].start_hour,sleep[i].start_min,sleep[i].end_hour,sleep[i].end_min);
}
}
}
return 0;
}