SDU_week10_B - 团队聚会(getline()读入大模拟+时间调度)

题目描述

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

解题思路

首先要明确一个问题,TA名字和做的事是没用的输入。问题转化为给定n个区间,求出满足条件(空闲的人【m-inBusy】>=2,忙的人【inBusy】<=1)的时间段。
首先定义两个结构体,分别为busy和free,busy指的是输入的时间段,free指的是空闲的时间段:

struct busy_
{
    // busy_(string time_ ,int label_)
    // {
    //     time=time_;
    //     label=label_;
    // }
    string time;
    int label;//某事件:1开始,0结束
}busy[4009];

struct free_
{
    free_(string time_ ,int label_)
    {
        time=time_;
        label=label_;
    }
    string time;
    int label;//空闲段:1开始,0结束
};

然后进行对所有时间点的输入,这时候时间段的概念已经不重要,我们只关心它是一个任务的开始点还是结束点,时间是什么时候。使用getline()读入一整行,然后提取有效信息存储。对所有时间点进行排序,第一关键词为时间,第二关键词为开始点优先。

然后维护一个在忙的人inBusy数据,初始化为0。对于所有时间点,每次读入一个时间点,开始点的话inBusy++,结束点的话inBusy–。然后再判断,若(m-inBusy>=2&&inBusy<=1)成立,则可以开会,若此时时间点的label和空闲段上一个符合要求的点的label不一样,则可以加入该点,从而保证free区间段的时间点成对出现。

在此之后进行边界值的合并,以及不满足要求(小于1小时)的时间段的删除。注意每一个操作都在空闲段不空的情况下操作。

注意

  1. getline()前要吞回车。
  2. 定义了构造函数必须初始化。
  3. 慎用删除erase,最好的解决方案是开一个新容器存储有效点。
  4. v.size()具有无符号的返回类型。从无符号0减去1会导致回绕到一些“很大”的无符号数字,所以用一个int的临时变量来接受它。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <stack>

#define _ ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// #include <bits/stdc++.h>
using namespace std;

struct busy_
{
    // busy_(string time_ ,int label_)
    // {
    //     time=time_;
    //     label=label_;
    // }
    string time;
    int label;//某事件:1开始,0结束
}busy[4009];

struct free_
{
    free_(string time_ ,int label_)
    {
        time=time_;
        label=label_;
    }
    string time;
    int label;//空闲段:1开始,0结束
};

bool cmp(busy_ a,busy_ b)
{
    if(a.time!=b.time) return a.time<b.time;
    else return a.label<b.label;//开始点堆在前面,从而维持inBusy较大
}

int main()
{
    // freopen("week10/a.txt","r",stdin); 
    int T;//T组数据
    cin>>T;
    for(int group=1;group<=T;group++)
    {
        for (int i = 0; i < 4009; i++)
        {
            busy[i].time="";
        }
        vector<busy_> timeStamp;//时间戳
        int m;//m个助教
        int sumThing=0;//事件总数
        cin>>m;

        //存时间戳
        for (int i = 0; i < m; i++)
        {
            int n;//一个人的n件事
            string temp;
            cin>>n;
            getchar();
            for (int i = 0; i < 2*n; i+=2)
            {//每次新增两个时间戳,一个开始一个结束
                getline(cin,temp);
                int cur=2*sumThing+i;

                busy[cur].label=1;
                busy[cur].time.push_back(temp[0]);
                busy[cur].time.push_back(temp[1]);
                busy[cur].time.push_back(temp[2]);
                busy[cur].time.push_back(temp[3]);
                busy[cur].time.push_back(temp[5]);
                busy[cur].time.push_back(temp[6]);
                busy[cur].time.push_back(temp[8]);
                busy[cur].time.push_back(temp[9]);
                busy[cur].time.push_back(temp[11]);
                busy[cur].time.push_back(temp[12]);
                busy[cur].time.push_back(temp[14]);
                busy[cur].time.push_back(temp[15]);
                busy[cur].time.push_back(temp[17]);
                busy[cur].time.push_back(temp[18]);
                timeStamp.push_back(busy[cur]);

                busy[cur+1].label=0;
                busy[cur+1].time.push_back(temp[20]);
                busy[cur+1].time.push_back(temp[21]);
                busy[cur+1].time.push_back(temp[22]);
                busy[cur+1].time.push_back(temp[23]);
                busy[cur+1].time.push_back(temp[25]);
                busy[cur+1].time.push_back(temp[26]);
                busy[cur+1].time.push_back(temp[28]);
                busy[cur+1].time.push_back(temp[29]);
                busy[cur+1].time.push_back(temp[31]);
                busy[cur+1].time.push_back(temp[32]);
                busy[cur+1].time.push_back(temp[34]);
                busy[cur+1].time.push_back(temp[35]);
                busy[cur+1].time.push_back(temp[37]);
                busy[cur+1].time.push_back(temp[38]);
                timeStamp.push_back(busy[cur+1]);
            }
            sumThing+=n;
        }
        sort(timeStamp.begin(),timeStamp.end(),cmp);

        //统计空闲时间
        int inBusy=0;//正忙的人,m>2时1及以下有效,m=2时0有效
        vector<free_> freeStamp;//空闲时间戳
        // if(m==2&&timeStamp[0].time!="18000101000000")
        //     freeStamp.push_back(free_("18000101000000",1));
        // else if(m==2&&timeStamp[0].time=="18000101000000")
        //     inBusy++;
        // else if(m>2 &&timeStamp[0].time!="18000101000000")
        //     freeStamp.push_back(free_("18000101000000",1));
        // else if(m>2 &&timeStamp[0].time=="18000101000000"&&timeStamp[1].time!="18000101000000")
        //     freeStamp.push_back(free_("18000101000000",1)),inBusy++;
        // else inBusy++;
        
        if(timeStamp[0].time!="18000101000000") freeStamp.push_back(free_("18000101000000",1));
        for(int i=0;i<2*sumThing;i++)
        {
            if(timeStamp[i].label==1)inBusy++;
            else inBusy--;

            int tmp;//伪装的结束符
            if(freeStamp.empty()) tmp=0;
            else tmp=freeStamp.back().label;
            if(m-inBusy>=2&&inBusy<=1)
            {
                if(timeStamp[i].time=="22000101000000") continue;
                if(tmp==0)
                    freeStamp.push_back(free_(timeStamp[i].time,1));
            }
            else
            {
                if(tmp==1)
                    freeStamp.push_back(free_(timeStamp[i].time,0));
            }
        }
        if(!freeStamp.empty())
        {
            // freeStamp.front().time="18000101000000";
            if(freeStamp.back().label==1)
                freeStamp.push_back(free_("22000101000000",0));   
        }

        //去除重复边界
        if(!freeStamp.empty())
        {
            int freeStampSize=freeStamp.size();
            for (int i = 1; i <=freeStampSize-3; i++)
            //v.size()具有无符号的返回类型。从无符号0减去1会导致回绕到一些“很大”的无符号数字(这是模运算)!!!
            {
                if(freeStamp[i].time==freeStamp[i+1].time)
                {
                    freeStamp[i].label=2;
                    freeStamp[i+1].label=2;
                }
            }
            vector <free_>::iterator iter = freeStamp.begin();
            while (iter!=freeStamp.end())
            {
                if(iter->label==2) freeStamp.erase(iter);
                else iter++;
            }
        }

        //选出大于1小时时间段
        //compare是str预留字
        vector<string> ans;
        if(!freeStamp.empty())
        {
            for(int i=0;i<freeStamp.size()-1;i+=2)
            {
                int lyear=atoi(freeStamp[i].time.substr(0,4).c_str());
                int ryear=atoi(freeStamp[i+1].time.substr(0,4).c_str());
                if(ryear-lyear>=2)
                {//year
                    ans.push_back(freeStamp[i].time);
                    ans.push_back(freeStamp[i+1].time);
                    continue;
                }
                int lmonth=atoi(freeStamp[i].time.substr(4,2).c_str());
                int rmonth=atoi(freeStamp[i+1].time.substr(4,2).c_str());
                rmonth+=(ryear-lyear)*12;
                if(rmonth-lmonth>=2)
                {//month
                    ans.push_back(freeStamp[i].time);
                    ans.push_back(freeStamp[i+1].time);
                    continue;
                }
                int lday=atoi(freeStamp[i].time.substr(6,2).c_str());
                int rday=atoi(freeStamp[i+1].time.substr(6,2).c_str());
                rday+=(rmonth-lmonth)*30;
                if(rday-lday>=2)
                {//day
                    ans.push_back(freeStamp[i].time);
                    ans.push_back(freeStamp[i+1].time);
                    continue;
                }
                int lhour=atoi(freeStamp[i].time.substr(8,2).c_str());
                int rhour=atoi(freeStamp[i+1].time.substr(8,2).c_str());
                rhour+=(rday-lday)*24;
                if(rhour-lhour>=2)
                {//hour
                    ans.push_back(freeStamp[i].time);
                    ans.push_back(freeStamp[i+1].time);
                    continue;
                }
                int lmin=atoi(freeStamp[i].time.substr(10,2).c_str());
                int rmin=atoi(freeStamp[i+1].time.substr(10,2).c_str());
                rmin+=(rhour-lhour)*60;
                int lsec=atoi(freeStamp[i].time.substr(12,2).c_str());
                int rsec=atoi(freeStamp[i+1].time.substr(12,2).c_str());
                rsec+=(rmin-lmin)*60;
                if(rsec-lsec>=3600)
                {
                    ans.push_back(freeStamp[i].time);
                    ans.push_back(freeStamp[i+1].time);
                    continue;
                }
            }
        }
        
        //终于可以输出了
        cout<<"Scenario #"<<group<<":"<<endl;
        if(ans.empty())
        {
            cout<<"no appointment possible"<<endl;
            cout<<endl;
            continue;
        }
        for (int i=0;i<ans.size()-1;i+=2)
        {//MM/DD/YYYY hh:mm:ss
            string l=ans[i];
            string r=ans[i+1];
            cout<<"appointment possible from ";
            cout<<l.substr(4,2)<<'/';
            cout<<l.substr(6,2)<<'/';
            cout<<l.substr(0,4)<<' ';
            cout<<l.substr(8,2)<<':';
            cout<<l.substr(10,2)<<':';
            cout<<l.substr(12,2);
            cout<<" to ";
            cout<<r.substr(4,2)<<'/';
            cout<<r.substr(6,2)<<'/';
            cout<<r.substr(0,4)<<' ';
            cout<<r.substr(8,2)<<':';
            cout<<r.substr(10,2)<<':';
            cout<<r.substr(12,2)<<' ';
            cout<<endl;
        }
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值