程序设计思维 大模拟-3

团队聚会

题目描述
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
解题思路
这题有多个难点,提高了整体的复杂性。首先边是读入和存储数据。对于这种带日期时间的数据,一般有两种处理方法:全部按分钟或者秒等题目给的最小单位算成一个大数表示时间,或者就是单纯的开结构体按年月日时分秒开变量,前一种方法好算,后一种方法好看。
对于本题来说,有一个判断时间区间一小时的要求,那里需要折合成一个总时间进行计算,但是输出则需要按照时间格式,所以这里还是选择用开结构体的方式存时间。
至于模拟的过程,先将所有事件的起始时间点和结束时间点排序,判断人数足够且时间满足条件的情况下输出,同时在获取了可以开会的时间段后挪动整个区间的端点去寻找下一个时间段,因为整个开会时间段已经确定,不能再考虑了。

#include<iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
typejdef long long ll;
struct TA
{
    int year,month,day,hour,mint,sec;
    TA(){}
    TA(int they,int themo,int thed,int theh,int themi,int thes)
    {
        year = they;
        month = themo;
        day = thed;
        hour = theh;
        mint = themi;
        sec = thes;
    }
    bool operator < (const TA &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(mint != b.mint)return mint < b.mint;
        return sec < b.sec;
    }
    bool operator <= (const TA &b)const {
        return !(b<*this);
    }
    TA& operator = (const TA &b){
        year = b.year;
        month = b.month;
        day = b.day;
        hour = b.hour;
        mint = b.mint;
        sec = b.sec;
        return *this;
    }
};
TA st,en,nullp,s[30][205],e[30][205],a[5001];
int n,m,tot,apnum[30],ansl[30],ansr[30];
string s0;
bool judge_1hour(int l,int r)//计算时间是否够一个小时
{
    ll h1,h2;
    h1=(ll)a[l].sec+60*(ll)a[l].mint+3600*(ll)a[l].hour+24*3600*(ll)a[l].day+720*3600*(ll)a[l].month+12*720*3600*(ll)a[l].year;
    h2=(ll)a[r].sec+60*(ll)a[r].mint+3600*(ll)a[r].hour+24*3600*(ll)a[r].day+720*3600*(ll)a[r].month+12*720*3600*(ll)a[r].year;
    if(h2-h1>=3600)
        return true;
    else
        return false;
}
bool judge_people_l(int l)
{
    int cnt=0;//可以参加会议的人数计数
    for(int i=0;i<m;++i){
        if(apnum[i]==0){
            cnt++;continue;
        }
        if(a[l]<=s[i][0]||(e[i][apnum[i]-1]<=a[l]&&a[l]<en)){//早于这个人的事件开始,或者上一个人事件结束但h还不到最终时间
            cnt++;continue;
        }
        for(int j=ansl[i];j<apnum[i]-1;j++){
            if(s[i][j]<=a[l]&&a[l]<e[i][j])//这个时间卡在了一个事件内,退出
                break;
            if(j+1<apnum[i]&&e[i][j]<=a[l]&&a[l]<s[i][j+1]){//注意会议的结束时间必须严格早于s[i][j+1],没有等号
                ansl[i]=j;
                cnt++;
                break;
            }
        }
    }
    if(cnt>=2&&(m-cnt)<=1)
        return true;
    else
        return false;
}
bool judge_people_r(int r)
{
    int cnt = 0;
    for(int i=0;i<m;i++)
    {
        if(apnum[i]==0)
        {
            cnt++;
            continue;
        }
        if((st<a[r]&&a[r]<=s[i][0])||(e[i][apnum[i]-1]<a[r]&&a[r]<=en))
        {
            cnt++;
            continue;
        }
        for(int j=ansr[i];j<apnum[i]-1;j++)
        {
            if(s[i][j]<a[r]&&a[r]<=e[i][j])
            {
                break;
            }
            if(j+1<apnum[i]&&e[i][j]<a[r]&&a[r]<=s[i][j+1])
            {
                ansr[i] = j;
                cnt++;
                break;
            }
        }
    }
    if(cnt>=2&&(m-cnt)<=1)
        return true;
    else
        return false;
}
void print(int l,int r)
{
    printf("appointment possible from ");
    printf("%02d/%02d/%04d %02d:%02d:%02d to ",
           a[l].month,a[l].day,a[l].year,a[l].hour,a[l].mint,a[l].sec);
    printf("%02d/%02d/%04d %02d:%02d:%02d\n",
           a[r].month,a[r].day,a[r].year,a[r].hour,a[r].mint,a[r].sec);
}
int main()
{
    int T;
    st = TA(1800,1,1,0,0,0);
    en = TA(2200,1,1,0,0,0);
    nullp = TA(0,0,0,0,0,0);
    scanf("%d",&T);
    for(int cas = 1;cas <= T;cas++)
    {
        printf("Scenario #%d:\n",cas);
        tot = 0;
        for(int i=0;i<=20;i++)
        {
            for(int j=0;j<apnum[i];j++)
            {
                s[i][j] = e[i][j] = nullp;
            }
            ansl[i] = ansr[i] = apnum[i] = 0;
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d",&n);
            apnum[i] = n;
            for(int j=0;j<n;j++)
            {
                int they,themo,theday,theh,themi,thesec;
                cin>>they>>themo>>theday>>theh>>themi>>thesec;
                s[i][j] = TA(they,themo,theday,theh,themi,thesec);
                cin>>they>>themo>>theday>>theh>>themi>>thesec;
                e[i][j] = TA(they,themo,theday,theh,themi,thesec);
                //scanf("%d%d%d%d%d%d",&s[i][j].year,&s[i][j].month,&s[i][j].day,&s[i][j].hour,&s[i][j].mint,&s[i][j].sec);
                //scanf("%d%d%d%d%d%d",&e[i][j].year,&e[i][j].month,&e[i][j].day,&e[i][j].hour,&e[i][j].mint,&e[i][j].sec);
                getline(cin,s0);
                a[tot++] = s[i][j];
                a[tot++] = e[i][j];
            }
        }
        a[tot++] = st;
        a[tot++] = en;
        int l = 0,r = 1;
        sort(a,a+tot);
        bool flag = false;
        while(l<tot&&r<tot)
        {
            while(r<tot&&judge_people_r(r))
            {
                r++;
            }
            r--;
            if(judge_1hour(l,r))
            {
                flag = true;
                print(l,r);
            }
            l = r + 1;
            while(l<tot&&!judge_people_l(l))
            {
                l++;
            }
            r = l + 1;
        }
        if(!flag)
        {
            printf("no appointment possible\n");
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值