团队聚会——时间管理

问题描述

TA团队每周都会有很多任务,有的可以单独完成,有的则需要所有人聚到一起,开过会之后才能去做。但TA团队的每个成员都有各自的事情,找到所有人都有空的时间段并不是一件容易的事情。

给出每位助教的各项事情的时间表,你的任务是找出所有可以用来开会的时间段。
【输入格式】
第一行一个数T(T≤100),表示数据组数。

对于每组数据,第一行一个数m(2 ≤ m ≤ 20),表示TA的数量。

对于每位TA,首先是一个数n(0≤ n≤100),表示该TA的任务数。接下来n行,表示各个任务的信息,格式如下

YYYY MM DD hh mm ss YYYY MM DD hh mm ss “some string here”

每一行描述的信息为:开始时间的年、月、日、时、分、秒;结束时间的年、月、日、时、分、秒,以及一些字符串,描述任务的信息。

数据约定:

所有的数据信息均为固定位数,位数不足的在在前面补前导0,数据之间由空格隔开。

描述信息的字符串中间可能包含空格,且总长度不超过100。

所有的日期时间均在1800年1月1日00:00:00到2200年1月1日00:00:00之间。

为了简化问题,我们假定所有的月份(甚至2月)均是30天的,数据保证不含有不合法的日期。

注意每件事务的结束时间点也即是该成员可以开始参与开会的时间点。
【输出格式】
对于每一组数据,首先输出一行"Scenario #i:",i即表明是第i组数据。

接下来对于所有可以用来开会的时间段,每一个时间段输出一行。

需要满足如下规则:

在该时间段的任何时间点,都应该有至少两人在场。
在该时间段的任何时间点,至多有一位成员缺席。
该时间段的时间长度至少应该1h。

所有的成员都乐意一天24h进行工作。

举个例子,假如现在TA团队有3位成员,TT、zjm、hrz。

那么这样的时间段是合法的:会议开始之初只有TT和zjm,后来hrz加入了,hrz加入之后TT离开了,此后直到会议结束,hrz和zjm一直在场。

要求:

输出满足条件的所有的时间段,尽管某一段可能有400年那么长。
时间点的格式为MM/DD/YYYY hh:mm:ss。
时间段的输出格式为"appointment possible from T0 to T1",其中T0和T1均应满足时间点的格式。
严格按照格式进行匹配,如果长度不够则在前面补前导0。
按时间的先后顺序输出各个时间段。
如果没有合适的时间段,输出一行"no appointment possible"。
每组数据末尾须打印额外的一行空行。

【样例输入】

2
3
3
2020 06 28 15 00 00 2020 06 28 18 00 00 TT study
2020 06 29 10 00 00 2020 06 29 15 00 00 TT solving problems
2020 11 15 15 00 00 2020 11 17 23 00 00 TT play with his magic cat
4
2020 06 25 13 30 00 2020 06 25 15 30 00 hrz play
2020 06 26 13 30 00 2020 06 26 15 30 00 hrz study
2020 06 29 13 00 00 2020 06 29 15 00 00 hrz debug
2020 06 30 13 00 00 2020 06 30 15 00 00 hrz play
1
2020 06 01 00 00 00 2020 06 29 18 00 00 zjm study
2
1
1800 01 01 00 00 00 2200 01 01 00 00 00 sleep
0

【样例输出】
Scenario #1:
appointment possible from 01/01/1800 00:00:00 to 06/25/2020 13:30:00
appointment possible from 06/25/2020 15:30:00 to 06/26/2020 13:30:00
appointment possible from 06/26/2020 15:30:00 to 06/28/2020 15:00:00
appointment possible from 06/28/2020 18:00:00 to 06/29/2020 10:00:00
appointment possible from 06/29/2020 15:00:00 to 01/01/2200 00:00:00

Scenario #2:
no appointment possible

问题分析

用三个结构体存储时间,一个存储TA以及其事件开始时间,一个存TA及其事件结束时间。还有一个存放混合总时间,只要保证所有的时间都是有序的,最后遍历总时间判断。
对于所给区间端点进行滑动判断。为这个时间点,遍历所有的TA,在其first或end结构体数组里找满足这个时间点内的端,找到这个时间点的TA个数即可。
1、对于时间区间,先滑动右端点,到达临界值时输出区间
2、滑动左端点,不满足的情况越过,直到临界值停止
要判断时间长度找,到的左右端点,还需要验证判断长度>1h才可输出。
若全部遍历完没有结果,则输出没有空闲时间安排。

#include <cstdio>
#include<iostream>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;

int num[25],cnt,n,T;
bool flag;
char c[2500];
struct time 
{
	int year,month,hour,day,minute,second;
	bool operator = (const time &p)
	{
		year =p.year;
		month=p.month;
		day=p.day;
		hour=p.hour;
		minute=p.minute;
		second=p.second;
	}
	bool operator < (const time &p)const
	{
		if(year!=p.year)return year<p.year;
		if(month!=p.month)return month<p.month;
		if(day!=p.day)return day<p.day;
		if(hour!=p.hour)return hour<p.hour;
		if(minute!=p.minute)return minute<p.minute;
		return second<p.second;
	}
	bool operator > (const time &p)const{return p<*this;}
	bool operator <= (const time &p)const{return !(p<*this);}
	bool operator >= (const time &p)const{return !(*this<p);}
	bool operator == (const time &p)const{return !(p<*this||*this<p);}
	bool operator != (const time &p)const{return p<*this||*this<p;}	
};
time first[25][150],last[25][150],history[4200],s,t;
bool lup(int pos)
{
	if(pos==cnt)
		return 0;
	int count=0;
	for(int i=1;i<=n;i++)
	{
		if(num[i]==0){count++;continue;}//TA没事 
		if(history[pos]<first[i][1])
		{
			count++;
			continue;
		}
		if(history[pos]>=last[i][num[i]]&&history[pos]<t)//当前时间在TA事情的最前边或最后边 
		{
			count++;
			continue;
		}
		for(int j=1;j<=num[i];j++)
		{
			if(history[pos]>=first[i][j]&&history[pos]<last[i][j])break;
			if(j+1<=num[i]&&history[pos]>=last[i][j]&&history[pos]<first[i][j+1])
			{
				count++;
				continue;
			}
		}
	}
	if(count>=2&&count>=n-1)return 1;
    else return 0;
}
bool rup(int pos)
{
	if(pos==0)return 0;
	int count=0;
	for(int i=1;i<=n;i++)
	{
		if(num[i]==0)
		{	
			count++;
			continue;
		}	
		if(history[pos]<=first[i][1]&&history[pos]>s)
		{
			count++;
			continue;
		}
		if(history[pos]>last[i][num[i]])
		{
			count++;
			continue;
		}
		if(history[pos]==last[i][num[i]])continue;
		for(int j=1;j<=num[i];j++)
		{
			if(history[pos]>first[i][j]&&history[pos]<=last[i][j])
				break;
			if(j+1<=num[i]&&history[pos]>last[i][j]&&history[pos]<=first[i][j+1])
			{
				count++;
				continue;
			}
		}
	}
	if(count>=2&&count>=n-1)return 1;
	return 0;
}
bool pd(int left,int right)
{
	time l,r;
	l=history[left];r=history[right];
	if(r.year-l.year>=2)return 1;
	r.month+=(r.year-l.year)*12;
	if(r.month-l.month>=2)return 1;
	r.day+=(r.month-l.month)*30;
	if(r.day-l.day>=2)return 1;
	r.hour+=(r.day-l.day)*24;
	if(r.hour-l.hour>=2)return 1;
	r.minute+=(r.hour-l.hour)*60;
	r.second+=(r.minute-l.minute)*60;
	if(r.second-l.second>=3600)return 1;
	return 0;
}
void print(int l)
{
	if(history[l].month<10)cout<<"0";
	cout<<history[l].month<<"/";
	if(history[l].day<10)cout<<"0";
	cout<<history[l].day<<"/";
	cout<<history[l].year<<" ";
	if(history[l].hour<10)cout<<"0";
	cout<<history[l].hour<<":";
	if(history[l].minute<10)cout<<"0";
	cout<<history[l].minute<<":";
	if(history[l].second<10)cout<<"0";
	cout<<history[l].second;
}
void output(int l,int r)
{
	if(!pd(l,r))return;
	flag=1;
	cout<<"appointment possible from ";
	print(l);
	cout<<" to ";
	print(r);
	cout<<endl;
}

int main()
{
	s.year=1800;s.month=1;s.day=1;s.hour=0;s.minute=0;s.second=0;
	t.year=2200;t.month=1;t.day=1;t.hour=0;t.minute=0;t.second=0;
	cin>>T;	 
	for(int k=1;k<=T;k++)
	{
		flag=0;
		memset(num,0,sizeof(num));
		memset(first,0,sizeof(first));
		memset(last,0,sizeof(last));
		memset(history,0,sizeof(history)); 
		cin>>n;
		cnt=0;
		for(int i=1;i<=n;i++)
		{
			cin>>num[i];
			for(int j=1;j<=num[i];j++)
			{
				cin>>first[i][j].year>>first[i][j].month>>first[i][j].day>>first[i][j].hour>>first[i][j].minute>>first[i][j].second;
			    cin>>last[i][j].year>>last[i][j].month>>last[i][j].day>>last[i][j].hour>>last[i][j].minute>>last[i][j].second;
				history[++cnt]=first[i][j];
				history[++cnt]=last[i][j];
				gets(c);                  
			}
		}	
		history[++cnt]=s;
		history[++cnt]=t;
		sort(history+1,history+1+cnt); 
		cout<<"Scenario #"<<k<<":"<<endl;
		int left=1,right=1;
		while(left<=cnt&&right<=cnt)
		{
			right++;
			if(right>cnt)
				break;
			while(right<=cnt&&rup(right))
				right++;
	   		 right--;               
			output(left,right);
			left=right+1;
			while(left<=cnt&&!lup(left))
				left++;
			right=left;
		}
		if(!flag)
			cout<<"no appointment possible"<<endl;
		cout<<endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值