WEEK10限时模拟-B-团队聚会

题目描述
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 Input
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
Simple OutputScenario #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

思路:
本题比较复杂,但是并不难,关键在于找每一个时间段有空的人数以及解决输入输出问题。
1.构建时间类date,年月日小时分钟秒,实现基本的运算符号重载。
2.初始化变量,输入每个成员事务的时间点,对所有时间点升序排序,从最左侧开始,首先确定空闲时间段最长的右边界,此时间满足最少两人有空且缺席的不能超过一个人。然后确定下一轮时间段。
代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=30000;
struct date{
	int tag;
	int y, M, d, h, m, s;
	date() {}
	date(int _y, int _M, int _d, int _h, int _m, int _s)
		:y(_y),M(_M),d(_d),h(_h),m(_m),s(_s) {}
	bool operator < (const date &x) const{
		if(y!=x.y) return y<x.y;
		if(M!=x.M) return M<x.M;
		if(d!=x.d) return d<x.d;
		if(h!=x.h) return h<x.h;
		if(m!=x.m) return m<x.m;
		return s<x.s; 
	}
	bool operator != (const date &x) const{
		if(y!=x.y || M!=x.M || d!=x.d ||
			h!=x.h || m!=x.m || s!=x.s)
			return true;
		else 
			return false;
	}
	bool operator == (const date &x) const{
		if(*this!=x) 
			return false;
		return true;
	}
	bool operator <= (const date &x) const{
		if(*this <x || *this ==x) 
			return true;
		return false;
	}
	date operator + (int x) {
		date temp = *this;
		temp.h+=x;
		while(temp.h>23) 
		{
	    temp.h-=24;
		temp.d++;
	}
		while(temp.d>30){
		temp.d-=30;
		 temp.M++;
	}
		while(temp.M>12){
		temp.M-=12;
		temp.y++;
	}
		return temp;
	}
	}a[maxn]; 
int idx,num;
vector<date> v;
string str;
int main()
{
	ios::sync_with_stdio(0);
	int T;
	cin>>T;
	for(int i=1;i<=T;i++){
		int m;
		cin>>m;
		idx=0;
		num=0;
		v.clear();
		for(int j=1;j<=m;j++){
			int n;
			cin>>n;
			for(int i=1;i<=n;i++){
				a[++idx].tag=1;
				cin>>a[idx].y>>a[idx].M>>a[idx].d>>a[idx].h>>a[idx].m>>a[idx].s;
				a[++idx].tag=0;
			    cin>>a[idx].y>>a[idx].M>>a[idx].d>>a[idx].h>>a[idx].m>>a[idx].s;
				getline(cin,str);
			}
		} 
		sort(a+1,a+idx+1);
	date st(1800,1,1,0,0,0);
	date ed(2200,1,1,0,0,0);
	int flag;
	if(a[1]==st) 
	flag = 0;
	else {
	flag = 1;
	v.push_back(st);
}
	for(int i=1;i<=idx;i++){
		date d(a[i].y, a[i].M, a[i].d, a[i].h, a[i].m, a[i].s);
		while(a[i]==d&&i<=idx){
			if(a[i].tag) 
			num++;
			else 
			num--;
			i++;
		}
		i--;
		if(flag){
			if((m==2&&num>=1)||(m>2&&num>1))
			{
				v.push_back(a[i]);
				flag=0; 
			}
		}
		else{
			if(a[i]!=ed&&((m>2&&num<=1)||(m==2&&num==0))){
				v.push_back(a[i]);
				flag=1;
			}
		}
	}
	if(flag) 
	v.push_back(ed);
		int lab=0;
		cout<<"Scenario #"<<i<<":"<<endl;
		for(int i=1;i<v.size();i+=2){
			if(v[i-1]+1<=v[i]){
				cout<<"appointment possible from ";
				if(v[i-1].M<10) 
				cout<<"0";
		        cout<<v[i-1].M<<"/";
		       if(v[i-1].d<10) 
			   cout<<"0";
		       cout<<v[i-1].d<<"/"<<v[i-1].y<<" ";
		       if(v[i-1].h<10) 
			   cout<<"0";
		        cout<<v[i-1].h<<":";
		      if(v[i-1].m<10) 
			  cout<<"0";
		         cout<<v[i-1].m<<":";
		       if(v[i-1].s<10) 
			   cout<<"0";
		        cout<<v[i-1].s;
		        cout<<" to ";
		        if(v[i].M<10) 
				cout<<"0";
		        cout<<v[i].M<<"/";
		       if(v[i].d<10) 
			   cout<<"0";
		       cout<<v[i].d<<"/"<<v[i].y<<" ";
		       if(v[i].h<10) 
			   cout<<"0";
		        cout<<v[i].h<<":";
		      if(v[i].m<10) 
			  cout<<"0";
		         cout<<v[i].m<<":";
		       if(v[i].s<10) 
			   cout<<"0";
		        cout<<v[i].s;
		        cout<<endl;
				lab=1;
			}
		}
		if(!lab) 
		cout<<"no appointment possible"<<endl;
		cout<<endl;
	}
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值