题目 团 队 聚 会
题目描述
TA团队每周都会有很多任务,有的可以单独完成,有的则需要所有人聚到一起,开过会之后才能去做。但TA团队的每个成员都有各自的事情,找到所有人都有空的时间段并不是一件容易的事情。
给出每位助教的各项事情的时间表,你的任务是找出所有可以用来开会的时间段。
input
第一行一个数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天的,数据保证不含有不合法的日期。
注意每件事务的结束时间点也即是该成员可以开始参与开会的时间点。
output
对于每一组数据,首先输出一行"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"。
每组数据末尾须打印额外的一行空行。
example
input 1
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
output 1
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
做法与思路
这个可以说是我近来花费时间最长的题目了,由于思路跟大部分人的思路不一样,又不想改变做法,还找不到自己的错误,就这样在南墙上撞了许久,最后还是选择了大部分人的做法。
言归正传,在解决这道题目前首先要为"时间"建立一个结构体,包括年、月、日、小时、分钟、秒,同时还要重载<等运算符方便比较时间前后。这一点大家基本上都是相同的。
然后在寻找空闲时间时产生的分歧,我个人的做法是分总人数等于2和大于2两类进行讨论,对于总人数为2,每个人都不能缺席,对于总人数大于2,最多缺席一人。把每个事务都用一条线在总时间轴上表示出来,对于总人数为2的情况,所有线覆盖的区间都要删除,剩余的且大于1小时的部分即为可选区间;对于总人数大于3,删除所有线出现重叠的部分,剩余且大于1小时的部分即为可选区间。我个人感觉这种思路没有什么问题,然而可能是代码上出现了一些我没有注意到的问题,所以总是wa。
于是换种做法,将所有的时间点都落到时间轴上,然后从左往右依次对时间段进行检定,如果该时间段上的人数满足要求,则记录该时间段,最后时间段中连续的部分合成起来,判断是否大于等于一小时,满足条件则可以输出该时间段。
不过记录时间段并合成操作最后完成有些麻烦,可以在对时间段检定的过程中使右端点尽可能向右(即把合成部分放在遍历过程中),下一次的左端点即在这个最右端点的左侧,这样在某种程度上方便一些。
对于判断时间是否大于等于一小时,可以直接把以Longlong结构判断,一分钟为60,一小时为3600以此类推,如果时间段右端点与左端点之差大于等于3600则符合条件。
代码
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct tim
{
long long y,m,d,h,f,s;
void get()
{
cin>>y>>m>>d>>h>>f>>s;
}
tim(int ty=0,int tm=0,int td=0,int th=0,int tf=0,int ts=0)
{
y=ty; m=tm; d=td; h=th; f=tf; s=ts;
}
bool operator < (const tim &b)const
{
if (y!=b.y) return y<b.y;
if (m!=b.m) return m<b.m;
if (d!=b.d) return d<b.d;
if (h!=b.h) return h<b.h;
if (f!= b.f) return f<b.f;
return s<b.s;
}
bool operator > (const tim &b)const { return b <*this; }
bool operator <= (const tim &b)const { return !(b <*this); }
bool operator == (const tim &b)const { return !(b <*this||*this<b);}
void print()
{
if(m<10) printf("0");
printf("%d/",m);
if(d<10) printf("0");
printf("%d/",d);
printf("%d ",y);
if(h<10) printf("0");
printf("%d:",h);
if (f<10) printf("0");
printf("%d:",f);
if (s<10) printf("0");
printf("%d",s);
}
};
const tim infi(1800,1,1,0,0,0),ed(2200,1,1,0,0,0);
tim a[30][110],b[30][110],t[5000];//分开记录开始时间点和结束时间点 ,t记录所有时刻点
bool manzu(tim &a,tim &b)
{
long long t1=a.s+60*a.f+3600*a.h+3600*24*a.d+3600*24*30*a.m+3600*24*30*12*a.y;
long long t2=b.s+60*b.f+3600*b.h+3600*24*b.d+3600*24*30*b.m+3600*24*30*12*b.y;
return (t1-t2>=3600);
}
int numble,flag,n;//分别记录时刻点的总数,有无符合条件的时刻点,人的总数
int num[25];
string muda;
bool judge_right(int index)
{
if(index==0) return 0;
int tot=0;
for(int i=1;i<=n;i++)
{
if(num[i]==0) tot++;
else if(t[index]<=a[i][1]) tot++;//比第一个起始点还左
else if(t[index]>b[i][num[i]]) tot++; //比最后一个结束点还右
else if(t[index]==b[i][num[i]]) continue;
else
for (int j=1;j<=num[i];j++)
{
if(t[index]>a[i][j]&&t[index]<=b[i][j]) break;
if (j+1<=num[i]&&t[index]>b[i][j]&&t[index]<=a[i][j+1]) {tot++; break; }
}
}
if(tot>=2&&tot>=n-1)return 1;
else return 0;
}
bool judge_left(int index)
{
if(index==numble) return 0;
int tot=0;
for(int i=1;i<=n;i++)
{
if(num[i]==0) tot++;
if(t[index]<a[i][1]) tot++;
if(b[i][num[i]]<=t[index]) tot++;
for(int j=1;j<=num[i];j++)
{
if(a[i][j]<=t[index]&&b[i][j]>t[index]) break;
if(j+1<=num[i]&&b[i][j]<=t[index]&&a[i][j+1]>t[index]) { tot++; break; }
}
}
if (tot>=2&&tot>=n-1) return 1;
else return 0;
}
void judge(int left, int right)
{
tim tem1=t[left],tem2=t[right];
//cout<<" "<<tem1.t<<" "<<tem2.t<<endl;
if(!manzu(tem2,tem1)) return;
flag=1;
printf("appointment possible from ");
tem1.print();
printf(" to ");
tem2.print();
printf("\n");
}
int main(int argc, char** argv)
{
int nnnn;
cin>>nnnn;
for(int ci=0;ci<nnnn;ci++)
{
numble=0;flag=0;
t[++numble]=infi;
t[++numble]=ed;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>num[i];
for(int j=1;j<=num[i];j++)
{
a[i][j].get();
b[i][j].get();
getline(cin,muda);
t[++numble]=a[i][j];
t[++numble]=b[i][j];
}
}
sort(t+1,t+1+numble);
printf("Scenario #%d:\n", ci+1);
int left=1,right=1;
while(right<=numble&&left<=right)
{
right++;
if(right>numble) break;
while(right<=numble&&judge_right(right)) right++;
right--;
judge(left, right);
left=right+1;
while(left<=numble&&!judge_left(left))left++;
right=left;
}
if (flag==0)printf("no appointment possible\n");
printf("\n");
}
return 0;
}