B -团队聚会(Week 10)

本文介绍了一种用于安排团队会议时间的算法,通过处理每位成员的任务时间表,找出满足至少两人在场、至多一位成员缺席且时长大于一小时的可用时间段。算法涉及时间点的排序、遍历和条件判断,解决了复杂的时间安排问题。

题目

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"。
每组数据末尾须打印额外的一行空行。
Simple Input2

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

Simple Output
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

题目思想

题目思路:首先定义一个结构体node,记录每一个时间点(年、月、日、时、分、秒、以及标记任务开始还是结束),存储在数组中,对时间进行排序,然后for循环从小到大开始对时间进行遍历;

如果a[i]这个时间点是一个任务的开始,那么inbusy(正在忙的人)就加一,
如果这个时间点是一个任务的结束,那么inbusy--;

维护一个inbusy值来记录当前时间正在忙的人的个数,然后开始判断当前时间是否可以加入结果vector中

在结果中,时间可能有两种身份

1、作为结果区间段的开始时间
2、作为结果区间段的结束时间

两种时间交替出现,交替加入结果vector中

注意细节:

总结一下这个题比较容易出错的点

1、要考虑到题目中给的时间区间,18000101000000-22000101000000;

对于右端点(18000101...)如果输入时间不是从右端点开始,要手动加入;
对于左端点(22000101...)不可以作为开始时间点加入到vector中

2、考虑在相同时间点有不同个任务开始和结束;

例如,有三位TA,在2020年6月25号0点,hrz开始睡觉直到2200年1月1号零点才醒,而TT从1800年1月1号开始打游戏在6月25号刚结束游戏,zjm始终是空闲。那么根据题意,聚会时间是18000101000000-22000101000000。

3、对于没有结果的输出,输出no appointment时,并不代表结果vector为空,有可能不为空,但时间都小于1h,导致没有输出;

4、最最重要的一点,wa了好久好久好久,就是不要用在函数命名的时候不要用关键字!

在比较两个待输出区间长度是否大于一个小时的函数can_len,原名是compare,但是compare是string的一个函数!改了名字就ac了

代码实现

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int maxn=5000;
int T,n,m,num,N,ok; 
struct node{
	string name;
	int tag;//tag=1任务开始tag=0任务结束 
	int y,mon,day,hour,minu,se;
	bool operator<(const node &p)const
	{
		if(y!=p.y) return y<p.y;
		if(mon!=p.mon) return mon<p.mon;
		if(day!=p.day) return day<p.day;
		if(hour!=p.hour) return hour<p.hour;
		if(minu!=p.minu) return minu<p.minu;
		return se<p.se; 
	}
	bool operator!=(const node &p)const
	{
		if(y!=p.y||mon!=p.mon||day!=p.day||hour!=p.hour||minu!=p.minu||se!=p.se)
			return true;
		else return false;
	}
	bool operator<=(const node &b)const 
		{return !(b<*this);}
}a[maxn];
vector<node> v;
void printff(node q);
void solve()
{
	while(!v.empty()) v.pop_back();
	bool flag;int inbusy=0;
	node start;//初始化左端点 
	start.y=1800;start.mon=1;start.day=1;
	start.hour=0;start.minu=0;start.se=0;
	node e;//初始化右端点 
	e.y=2200;e.mon=1;e.day=1;
	e.hour=0;e.minu=0;e.se=0;
	if(!(a[0]!=start)){
		flag=false;
	}
	else{
		v.push_back(start);
		flag=true;
	} 
	node right;
	for(int i=0;i<=num;i++)
	{
		right=a[i];
		while(a[i]<=right&&i<=num){
			if(a[i].tag){
				inbusy++;
			}
			else{
				inbusy--;
			}
			i++;
		}
		i--;
		if(flag){//放入结束时间 
			if((m==2&&inbusy>0)||(m>2&&inbusy>1))
			{
				v.push_back(a[i]);
				flag=false; 
			} 		
		}
		else{//放入开始时间 
			if(!(a[i]!=e)) continue;//最后的时间不允许放入开始时间中 
			if((m>2&&inbusy<=1)||(m==2&&inbusy==0))
			{
				v.push_back(a[i]);
				flag=true;
			}
		}
	}
	if(flag) v.push_back(e);
} 
bool can_len(node o,node p)
{
	p.hour+=1;
	if(p.hour>23){
		p.hour-=24;
		p.day+=1;
	} 
	if(p.day>30){
		p.day-=30;
		p.mon+=1;
	}
	if(p.mon>12){
		p.mon-=12;
		p.y+=1;
	} 
	if(!(o!=p) || p<o) return true;
	else return false;
}
void printff(node q)
{
	if(q.mon<10) cout<<"0";
	cout<<q.mon<<"/";
	if(q.day<10) cout<<"0";
	cout<<q.day<<"/";
	cout<<q.y<<" ";
	if(q.hour<10) cout<<"0";
	cout<<q.hour<<":";
	if(q.minu<10) cout<<"0";
	cout<<q.minu<<":";
	if(q.se<10) cout<<"0";
	cout<<q.se;
}
void print()
{
	int lv=v.size();
	for(int t=1;t<lv;t+=2)
	{
		if(can_len(v[t],v[t-1]))
		{//MM/DD/YYYY hh:mm:ss
			cout<<"appointment possible from ";
			printff(v[t-1]);
			cout<<" to ";
			printff(v[t]);
			cout<<endl;
			ok=1;
		}
	}
}
int main()
{
	//freopen("a.txt","r",stdin);
	cin>>T;
	string s;
	while(T--)
	{
		cin>>m;
		num=-1;ok=0;
		int l=m;
		while(l--)
		{
			cin>>n;
			getchar();
			for(int i=0;i<n;i++)
			{
				a[++num].tag=1;
				scanf("%d%d%d%d%d%d",&a[num].y,&a[num].mon,&a[num].day,&a[num].hour,&a[num].minu,&a[num].se);
				a[++num].tag=0;
				scanf("%d%d%d%d%d%d",&a[num].y,&a[num].mon,&a[num].day,&a[num].hour,&a[num].minu,&a[num].se);
				getline(cin,s);
			}
		} 
		sort(a,a+num+1);
		solve();
		
		N++;
		cout<<"Scenario #"<<N<<":"<<endl;
		print();
		if(ok==0) cout<<"no appointment possible"<<endl;
		cout<<endl;
	}
	return 0;
} 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值