week10限时大模拟_B -团队聚餐

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

题目思路

这个题目是一个比较大的模拟题。在这个题目里面,我定义了一个TIme结构体用来存储时间年月日时分秒,并重构了一些比较用的运算符。

struct Time{
	int year,month,day,hour,minute,second;		//年月日时分秒 
	Time(){}
	Time(int y,int mo,int d,int h,int mi,int s){}
	bool operator == (const Time& p)const{}
	bool operator != (const Time& p)const{}
	bool operator < (const Time& p)const{}
	bool operator > (const Time& p)const{}
	bool operator >= (const Time& p)const{}
	bool operator <= (const Time& p)const{}
	}

然后定义三个数组,两个二维数组s和e分别存储每个助教任务的开始时间和结束时间,一个一维数组t存储所有的时间点。然后再将t数组排序,这里的t数组我们也可以使用set实现。
然后我们使用双指针来查找空闲时间段。我使用了两个函数check_left和check_right来判断左右端点是否是处于空闲段。确定了左右端点后判断时间长度是否超过一个小时。
最后如果满足条件输出即可。

代码实现

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
#define _for(i,a,b) for(int i = (a); i < (b); i++)
#define _rep(i,a,b) for(int i = (a); i <= (b); i++)
#define ll long long

int T,m,n;
struct Time{
	int year,month,day,hour,minute,second;		//年月日时分秒 
	Time(){}
	Time(int y,int mo,int d,int h,int mi,int s){
		year = y,month = mo,day = d,hour = h,minute = mi,second = s;
	}
	bool operator == (const Time& p)const{
		if(year == p.year && month == p.month && day == p.day && hour == p.hour && minute == p.minute && second == p.second) return true;
		else return false;
	}
	bool operator != (const Time& p)const{
		return !(*this == p);
	}
	bool operator < (const Time& p)const{
		if(year != p.year) return year < p.year;
		else if(month != p.month) return month < p.month;
		else if(day != p.day) return day < p.day;
		else if(hour != p.hour) return hour < p.hour;
		else if(minute != p.minute) return minute < p.minute;
		else return second < p.second; 
	}
	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 !(*this > p); 
	}
	
}s[25][120],e[25][120],t[4200]; 
int num[25],cnt,flag; 

Time begin(1800,1,1,0,0,0),end(2200,1,1,0,0,0);

void init(){
	memset(s,0,sizeof(s));
	memset(e,0,sizeof(e));
	memset(num,0,sizeof(num));
	flag = 0;
	cnt = 0;
	t[++cnt] = begin;
	t[++cnt] = end;
}

bool check_left(int x){			//判断左端点是否空闲 
	if(x == cnt) return false;
	int tmp = 0;				//记录满足条件的TA个数 
	_rep(i,1,m){
		if(num[i] == 0){		//第i个TA任务数为0 
			tmp++;
			continue;
		}
		if(t[x] < s[i][1]){		//比起始任务时间小 
			tmp++;
			continue;
		}
		if(t[x] >= e[i][num[i]] && t[x] < end){		//比最后一个任务时间大 
			tmp++;
			continue;
		}
		_rep(j,1,num[i]){		
			if(t[x] >= s[i][j] && t[x] < e[i][j]) break;
			if (j + 1 <= num[i] && e[i][j] <= t[x] && s[i][j + 1] > t[x]) { tmp++; break; }
		}
		
	}
	if(tmp >= 2 && tmp >= m-1) return true;
	else return false;
}

bool check_right(int x){
	if(x == 0) return false;
	int tmp = 0;
	_rep(i,1,m){
		if(num[i] == 0){
			tmp++;
			continue;
		}
		if(t[x] <= s[i][1] && t[x] > begin){
			tmp++;
			continue;
		}
		if(t[x] > e[i][num[i]]){
			tmp++;
			continue;
		}
		if (t[x] == e[i][num[i]])continue;

		_rep(j,1,num[i]){
			if(t[x] > s[i][j] && t[x] <= e[i][j]) break;
			if (j + 1 <= num[i] && e[i][j] < t[x] && s[i][j + 1] >= t[x]) { tmp++; break; }
		}
		
	}
	if(tmp >= 2 && tmp >= m-1) return true;
	else return false;
}

bool check_anhour(int l,int r){
	int lefth = ((t[l].year * 12 + t[l].month) * 30 + t[l].day) * 24 + t[l].hour;
	int righth = ((t[r].year * 12 + t[r].month) * 30 + t[r].day) * 24 + t[r].hour;
	int thehour = righth - lefth;
	
	if(thehour >= 2) return true;
	else if(thehour == 1){
		int lefts = t[l].minute * 60 + t[l].second;
		int rights = t[r].minute * 60 + t[r].second;
		int thesecond = rights - lefts;
		
		if(thesecond >= 0)return true;
		else return false;
	}
	else return false;
}

void output(int left,int right){
	flag = 1;
	printf("appointment possible from ");
	printf("%02d/%02d/%04d %02d:%02d:%02d",t[left].month,t[left].day,t[left].year,t[left].hour,t[left].minute,t[left].second);
	printf(" to ");
	printf("%02d/%02d/%04d %02d:%02d:%02d\n",t[right].month,t[right].day,t[right].year,t[right].hour,t[right].minute,t[right].second);
	
}

int main(){
	scanf("%d",&T);
	_rep(i,1,T){		//组数 
		init();
		scanf("%d",&m);
		_rep(j,1,m){	//TA数 
			scanf("%d",&n);
			num[j] = n;
			_rep(k,1,n){//每个TA的任务数 
				scanf("%d%d%d%d%d%d",&s[j][k].year,&s[j][k].month,&s[j][k].day,&s[j][k].hour,&s[j][k].minute,&s[j][k].second);
				scanf("%d%d%d%d%d%d",&e[j][k].year,&e[j][k].month,&e[j][k].day,&e[j][k].hour,&e[j][k].minute,&e[j][k].second);
				t[++cnt] = s[j][k];
				t[++cnt] = e[j][k];
				string empty;
				getline(cin,empty);
			}
		}
		sort(t+1,t+cnt+1);
		printf("Scenario #%d:\n",i);
		int left = 1,right = 0;
		while(left <=  cnt && right <= cnt){
			right++;
			
			while(right <= cnt && check_right(right)){
				right++;
			}
			right--;
			
			if(check_anhour(left,right)) output(left,right);
			left = right + 1;
			
			while(left <= cnt && !check_left(left)){
				left++;
			}
			right = left;
		}
		if(flag == 0){
			printf("no appointment possible\n");
		}
		printf("\n");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值