Problem Description
There is a cat, cat likes to sleep.
If he sleeps, he sleeps continuously no less than A hours.
For some strange reason, the cat can not stay awake continuously more than B hours.
The cat is lazy, it could sleep all the time,
but sometimes interesting events occur(such as cat food, TV show, etc) .
The cat loves these events very much.
So, Please help the cat plan their day so as not to miss any interesting events.
Every day the cat wants to live on the same schedule.
Input
The first line of the input file contains two integers A and B (1 <= A, B <= 24).
The second line of the input file contains the number n, the number of interesting events (1 <= n <= 20).
Following n rows describe the interesting events.
Each event is described line of the form hh:mm-hh:mm, which specifies
the time period during which it occurs. Time varies from 00:00 to 23:59.
No two interesting events will overlap.
If the event is completed earlier than the beginning, This means that it captures midnight.
The event is considered to be occupying the whole minute,
when it begins and the moment when it ends (event 12:00-13:59 lasted exactly 120 minutes). Start time and time end of the event are different.
Output
If the cat can organize their day so that during all the interesting events not sleep, print to output file Yes.
On the second line Bring k - how many times a day cat should go to bed.
In the following k rows Bring out the intervals in which the cat sleeps in the same format, in which interesting events are set in the input file. If making a few, display any.
If the cat can not organize their day desired way, print to the output file No.
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
思路
这道题的思路比较简单,由于输入的时间表是保证不重叠的且数据量比较少,逐个枚举判断即可
- 设置一个变量来存储循环过程中连续工作的时间
- 每次遍历到一个节目就将该节目的时长加入到连续工作的时间当中,若大于 B 则凉凉了,直接退出
- 当前节目的结束与下一个节目的开始这段时间若大于等于 A 则可以睡觉,否则加入连续工作的时间
- 若小于 A 则清空连续工作的时间
- 其余的只需要注意跨夜的判断以及时间为闭区间
代码实现
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int day=24*60;
struct node{
int s,e;
node(){}
node(int ss,int ee):s(ss),e(ee){}
bool operator<(const node &t)const{return s<t.s;}
}a[30];
vector<node> res;
int A,B,N,sum_1,sum_2;
void Output(node t){
printf("%d%d:%d%d-%d%d:%d%d\n",
t.s/600, t.s/60%10, t.s%60/10, t.s%10,
t.e/600, t.e/60%10, t.e%60/10, t.e%10);
}
int main()
{
while(~scanf("%d%d%d", &A, &B,&N)){
A*=60;
B*=60;
bool flag=true;
for (int i=1; i<=N; i++){
int h1,m1,h2,m2;
scanf("%02d:%02d-%02d:%02d",&h1,&m1,&h2,&m2);
a[i].s=h1*60+m1;a[i].e=h2*60+m2;
if(a[i].e<a[i].s) a[i].e+=day;
if(a[i].e-a[i].s+1>B) flag=false;
}
if(!flag){
printf("No\n");
continue;
}
sort(a+1,a+N+1);
a[N+1].s=a[N].e+1;
a[N+1].e=a[1].s+day-1;
res.clear();
sum_1=0,sum_2=0;
for(int i=1;i<=N;i++){
sum_1+=a[i].e-a[i].s+1;
if(sum_1>B){
flag=false;
break;
}
if(i<N)
sum_2=a[i+1].s-a[i].e-1;
else
sum_2=a[i+1].e-a[i].e;
if(sum_2>=A){
sum_1=0;
if(i<N)
res.push_back(node((a[i].e+1)%day,(a[i+1].s-1)%day));
else
res.push_back(node((a[i].e+1)%day,(a[i+1].e)%day));
}
else{
sum_1+=sum_2;
if(sum_1>B){
flag=false;
break;
}
}
}
if(flag&&res.size()>0){
printf("Yes\n%d\n", res.size());
for(int i=0;i<res.size();i++)
Output(res[i]);
}
else
printf("No\n");
}
return 0;
}
限时测试的时候写的是下面这份代码,没看出来哪里有问题,只好放弃debug重写了上面那一份,我感觉可能是 day 那里没处理好…
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
struct node{
int day,sh,sm,eh,em;
node(){}
node(int aa,int bb,int cc,int dd):sh(aa),sm(bb),eh(cc),em(dd){day=0;}
node(int aa,int bb,int cc,int dd,int ee):sh(aa),sm(bb),eh(cc),em(dd){day=ee;}
bool operator<(const node &s) const{
if(day!=s.day)
return day<s.day;
return (sh*60+sm)<(s.sh*60+s.sm);
}
}a[30];
vector<node> res;
int A,B,N;
void addminute(int &h_1,int &m_1){
if(m_1+1>=60){
m_1=m_1+1-60;
h_1++;
if(h_1>23){
h_1=0;
}
}
else
m_1++;
}
void subminute(int &h_2,int &m_2){
if(m_2-1<0){
h_2--;
m_2=m_2-1+60;
if(h_2<0){
h_2=23;
}
}
else
m_2--;
}
int sumtime_1(int sh,int sm,int eh,int em){
int s,e,ans;
if(eh*60+em<sh*60+sm) eh+=24;
s=sh*60+sm;
e=eh*60+em;
ans=e-s+1;
return ans;
}
int sumtime_2(int sh,int sm,int eh,int em){
addminute(sh,sm);
subminute(eh,em);
return sumtime_1(sh,sm,eh,em);
}
int main()
{
while(~scanf("%d%d%d",&A,&B,&N)){
res.clear();
A=A*60;B=B*60;
bool flag=true;
for(int i=1;i<=N;i++){
scanf("%d:%d-%d:%d",&a[i].sh,&a[i].sm,&a[i].eh,&a[i].em);
if(sumtime_1(a[i].sh,a[i].sm,a[i].eh,a[i].em)>B) flag=false;
if(a[i].sh*60+a[i].sm>a[i].eh*60+a[i].em)
a[i].day=2;
else
a[i].day=1;
}
if(!flag){
printf("No\n");
continue;
}
sort(a+1,a+1+N);
int h_1=a[N].eh,m_1=a[N].em,h_2=a[1].sh,m_2=a[1].sm;
addminute(h_1,m_1);
subminute(h_2,m_2);
a[N+1]=node(h_1,m_1,h_2,m_2);
int sum_1=0,sum_2=0;
for(int i=1;i<=N;i++){
sum_1+=sumtime_1(a[i].sh,a[i].sm,a[i].eh,a[i].em);
if(sum_1>B){
flag=false;
break;
}
if(i<N)
sum_2=sumtime_2(a[i].eh,a[i].em,a[i+1].sh,a[i+1].sm);
else
sum_2=sumtime_2(a[i].eh,a[i].em,a[i+1].eh,a[i+1].em)+1;
if(sum_2>=A){
int hh_1=a[i].eh,mm_1=a[i].em;
addminute(hh_1,mm_1);
if(i<N){
int hh_2=a[i+1].sh,mm_2=a[i+1].sm;
subminute(hh_2,mm_2);
res.push_back(node(hh_1,mm_1,hh_2,mm_2));
}
else
res.push_back(node(hh_1,mm_1,a[i+1].eh,a[i+1].em));
sum_1=0;
}
else{
sum_1+=sum_2;
if(sum_1>B){
flag=false;
break;
}
}
}
if(flag&&res.size()>0){
printf("Yes\n");
printf("%d\n",res.size());
for(int i=0;i<res.size();i++)
printf("%02d:%02d-%02d:%02d\n",res[i].sh,res[i].sm,res[i].eh,res[i].em);
}
else{
printf("No\n");
}
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)