程序设计思维与实践 Week10 限时大模拟

B - 团 队 聚 会 (不支持C++11)

题目描述

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组数据。

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

需要满足如下规则:

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

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

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

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

要求:

  1. 输出满足条件的所有的时间段,尽管某一段可能有400年那么长。
  2. 时间点的格式为MM/DD/YYYY hh:mm:ss。
  3. 时间段的输出格式为"appointment possible from T0 to T1",其中T0和T1均应满足时间点的格式。
  4. 严格按照格式进行匹配,如果长度不够则在前面补前导0。
  5. 按时间的先后顺序输出各个时间段。
  6. 如果没有合适的时间段,输出一行"no appointment possible"。
  7. 每组数据末尾须打印额外的一行空行。
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,内含int year, month, day, hour, minute, second;时间的信息。其中进行了封装,同时重载了相关的运算付哈,方便后期的使用。
  • 步骤依次为:
    1. 将所有出现的时间点投射到t数组上,同时记录所有时间段的对应不同人的起始点s和结束点e。
    2. 然后要将t进行排序,从第一段开始依次进行验证能否满足条件。用checkPer函数来判断该段时间是否满足i这个人空闲的条件,即该段时间是否在相应i人的s和e之间,如果在该人某一段的s和e之前,说明此人这段时间没有空,依次遍历每个人在这段时间是否有空。其中每次checkPer有一次空,则记录tot++,用来记录有空的人数,看最终人数是否满足可以开会的要求。然后右端点要往t的下一个数移动,继续判断这段时间各个人的时间是否满足,找到最长的时间段的右端点。
    3. 得出这最长一段时间有满足条件的人空闲后,还要判断开会的时间段是否满足1h
    4. 然后重新找到满足条件的时间段的左端点,遍历t中各个时间段,得到最终所有满足条件的时间段。

C++

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

struct Time {
	int year, month, day, hour, minute, second;
	Time() {}
	Time(int y, int mon, int d, int h, int min, int s) {
		year = y, month = mon, day = d, hour = h, minute = min, second = s;
	}
	//符号全部重载后方便后面大小的判断
	bool operator < (const Time &b)const
	{
		if (year != b.year)return year < b.year;
		if (month != b.month)return month < b.month;
		if (day != b.day)return day < b.day;
		if (hour != b.hour)return hour < b.hour;
		if (minute != b.minute)return minute < b.minute;
		return second < b.second;
	}
	bool operator>(const Time &b) const { return b < *this; }
	bool operator<=(const Time &b) const { return !(b < *this); }
	bool operator>=(const Time &b) const { return !(*this < b); }
	bool operator==(const Time &b) const { return !(b < *this || *this < b); }
	bool operator!=(const Time &b) const { return b < *this || *this < b; }

	void output() {//输出格式化
		if (month < 10)printf("0");
		printf("%d/", month);
		if (day < 10)printf("0");
		printf("%d/", day);
		printf("%d ", year);
		if (hour < 10)printf("0");
		printf("%d:", hour);
		if (minute < 10)printf("0");
		printf("%d:",minute);
		if (second < 10)printf("0");
		printf("%d", second);
	}
}s[25][120], e[25][120], t[4020];
int T, num[25], cnt, n, flag = 0;
Time tbegin(1800, 1, 1, 0, 0, 0), tend(2200, 1, 1, 0, 0, 0);

void init() {
	memset(num, 0, sizeof(num));
	memset(s, 0, sizeof(s));
	memset(e, 0, sizeof(e));
	memset(t, 0, sizeof(t));
}
bool checkPer(int id, Time l, Time r) {
	for (int i = 1; i <= num[id]; ++i) {
		if (l >= s[id][i] && r <= e[id][i])return 0;
	}
	return 1;
}
bool check(Time l, Time r) {
	if (l == r)return true;
	int tot = 0;
	for (int i = 1; i <= n; ++i) {
		if (checkPer(i, l, r))++tot;
	}
	return (tot >= n - 1 && tot >= 2);//同时要满足人数要求
}
//满足1h长
bool anhour(int left, int right) {
	Time l = t[left], r = t[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 judge(int left, int right) {
	if (!anhour(left, right))return;
	flag = 1;
	printf("appointment possible from ");
	t[left].output();
	printf(" to ");
	t[right].output();
	printf("\n");
}

int main()
{
	cin >> T;
	for (int cas = 1; cas <= T; cas++) {
		init();
		cnt = 0;
		flag = 0;
		t[++cnt] = tbegin;//记得要加两个临界点
		t[++cnt] = tend;
		cin >> n;
		for (int i = 1; i <= n; i++) {
			cin >> num[i];
			for (int j = 1; j <= num[i]; j++) {
				cin >> s[i][j].year >> s[i][j].month >> s[i][j].day >> s[i][j].hour >> s[i][j].minute >> s[i][j].second;
				cin >> e[i][j].year >> e[i][j].month >> e[i][j].day >> e[i][j].hour >> e[i][j].minute >> e[i][j].second;
				t[++cnt] = s[i][j];
				t[++cnt] = e[i][j];
				string meiyong;//没有用
				getline(cin, meiyong);
			}
		}
		sort(t + 1, t + 1 + cnt);//将时间段进行排序
		printf("Scenario #%d:\n", cas);
		int left = 1, right = 1;
		while (left <= cnt && right <= cnt) {
			if (right > cnt)break;
			//找到使时间段最长的右边界
			while (right <= cnt && check(t[right],t[right + 1]))right++;
			judge(left, right);
			left = right + 1;
			//确定左边界
			while (left <= cnt && !check(t[left], t[left + 1]))left++;
			right = left;
		}
		if (flag == 0)printf("no appointment possible\n");
		printf("\n");
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值