A - cat sleeping problem (week14 simulation)

subject

Sample Input

12 12 1
23:00-01:00
3 4 3
07:00-08:00
11:00-11:09
19:00-19:59

Sample Output

Yes
1
01:07-22:13
No

Summary and thinking

1. To understand the meaning of the question accurately, at the beginning, I didn't fully understand the meaning of the question, which led to the confusion of writing the question, so I understood the question as a schedule within 1-2 days;
2. The unified time format is expressed in minutes. At the beginning, it tried to use the form of hours: minutes. However, there were some problems in the opening and closing intervals and time comparison, which led to wa. Therefore, the next time for this kind of time comparison problem, it is better to convert the time into an integer;
3. Pay attention to the time range of 0-24 * 60, take the mold in time to prevent the output result from being wrong;
4. The realization process of this question is to sort the watching time, and then traverse two data each time to judge whether the time period between watching TV meets the sleeping time, if it meets the sleeping time, otherwise, judge whether it exceeds the maximum waking time, if it exceeds the maximum waking time, it means that it does not meet the question meaning, and output 'no'.
5. Don't forget to judge whether the first time and the last time (i.e. the time of the previous day) of watching the play meet the question.

code implementation

#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
const int day_time=24*60;
int A,B,n;
struct node{
	int s,e;//Start time and end time
	bool operator<(const node&p)const
	{
		return s<p.s;
	}
}a[25];
vector<node> v;
void _printf(node q)
{
	q.s=(q.s+day_time)%day_time;
	int sh=q.s/60;
	int sm=q.s%60;
	if(sh<10) cout<<"0";
	cout<<sh<<":";
	if(sm<10) cout<<"0";
	cout<<sm;
	cout<<"-";
	q.e=(q.e+day_time)%day_time;
	int eh=q.e/60;
	int em=q.e%60;
	if(eh<10) cout<<"0";
	cout<<eh<<":";
	if(em<10) cout<<"0";
	cout<<em; 
	cout<<endl;
}
int main()
{
	while(cin>>A>>B>>n)
	{
		memset(a,0,sizeof(a));
		v.clear();
		bool flag =true;
		for(int i=0;i<n;i++)
		{
			string str;
			cin>>str;
			a[i].s=((str[0]-'0')*10+(str[1]-'0'))*60+((str[3]-'0')*10+(str[4]-'0'));
			a[i].e=((str[6]-'0')*10+(str[7]-'0'))*60+((str[9]-'0')*10+(str[10]-'0'));
			if(a[i].e<a[i].s) a[i].e+=day_time;
			if(a[i].e-a[i].s+1 > B*60) flag=false;
		}
		if(!flag)
		{
			cout<<"No"<<endl;
			continue;
		}
		sort(a,a+n);
		node tmp,last;
		last.s=a[0].s;last.e=a[0].e;
		for(int i=1;i<n;i++)
		{
			if(a[i].s-last.e-1 >= A*60)
			{
				tmp.s=last.e+1;
				tmp.e=a[i].s-1;
				if(tmp.s!=tmp.e) v.push_back(tmp);
				last.s=a[i].s;last.e=a[i].e;
				continue;
			}
			last.e=a[i].e;
			if(last.e-last.s+1> B*60)
			{
				flag=false;
				break;
			}
		}
		if(a[0].s-last.e+day_time-1>=A*60)//A circle 
		{
			tmp.s=(last.e+1)%day_time;
			tmp.e=(a[0].s+day_time-1)%day_time;
			if(tmp.s!=tmp.e) v.push_back(tmp);
		}
		if(!flag || v.size()==0 || ((v[0].s-1+day_time)%day_time-last.s+1 > B*60))
		{
			cout<<"No"<<endl;
			continue;
		}
		cout<<"Yes"<<endl;
		cout<<v.size()<<endl;
		for(int i=0;i<v.size();i++)
			_printf(v[i]);
	}
	return 0;
}

Posted on Thu, 04 Jun 2020 13:36:15 -0400 by VanPEP