CSWek10模拟 Time period selection

问题描述:

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"。
每组数据末尾须打印额外的一行空行。

Sample 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

Sample 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

解题思路:

首先一定要构造时间结构体Tasktime,为每个成员开辟一个数组,存储该成员的每个任务的起始时间和结束时间,其次还要有一个数组t存储所有成员的全部起始时间和终止时间,可以知道,会议的起始时间和终止时间只会来自t,所以对t数组里的时间进行遍历,对于每一个时间,看是否有足够多成员满足空闲状态,如何判断是否空闲呢?只要该时间不在成员的一个任务时间内,即该成员的所有任务时间都不包含这个被判断的时间,该成员就有空。找到每一个会议时间采用了尺取法的思想。

实验代码:

#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
struct time
{
	int yy,mo,dd,hh,mi,ss;
    time(int y=0,int m1=0,int d=0,int h=0,int m2=0,int s=0):yy(y),mo(m1),dd(d),hh(h),mi(m2),ss(s){}
    bool operator < (const time &t) const
    {
        if (yy != t.yy)return yy < t.yy;
        if (mo != t.mo)return mo < t.mo;
        if (dd != t.dd)return dd < t.dd;
        if (hh != t.hh)return hh < t.hh;
        if (mi != t.mi)return mi < t.mi;
        return ss < t.ss;
    }
	bool operator >= (const time &t) const{return !(*this < t);}		
	bool operator > (const time& t) const{return t < *this;} 
    bool operator <= (const time& t) const{return !(*this > t);}
    bool operator !=(const time& t) const{return (*this < t||*this >t);}
    bool operator ==(const time& t) const{return *this != t;} 
    void output()
    {
    	printf("%02d/%02d/%04d %02d:%02d:%02d",mo,dd,yy,hh,mi,ss);
	}
}per_start[25][105],per_end[25][105],t[4010],start_time(1800, 1, 1, 0, 0, 0),end_time(2200, 1, 1, 0, 0, 0);
int per_num;
int num_of_time;
int num_of_task[25];
bool judge_end(int x)
{
    int count = 0;
    bool flag=1;		
    for (int i = 1; i <= per_num; i++) 
	{
		flag=1;
        for (int j = 1; j <= num_of_task[i]; j++) 
		{
            if (t[x] > per_start[i][j] && t[x] <= per_end[i][j])
			{
				flag=0;		
				break;	
			}	
        }
        if(flag)	count++;
    }
    if (count>=2&&per_num-count<=1)return true;
    return false;
}
bool judge_start(int x)
{
    int count = 0;
    bool flag=1;
    for (int i = 1; i <= per_num; i++) 
	{
		flag=1;
        for (int j = 1; j <= num_of_task[i]; j++) 
		{
            if (t[x] >= per_start[i][j] && t[x] < per_end[i][j])
			{
				flag=0;
				break;
			}
		}        
		if(flag)	count++; 
    }
    if(count>=2&&per_num-count<=1)return true;
    return false;
}
bool able_1h(int l,int r)		
{
	time left=t[l],right=t[r];
    if(right.yy>left.yy+1)return true;
    right.mo+=12*(right.yy-left.yy);
    if(right.mo>left.mo+1)return true;
    right.dd+=30*(right.mo-left.mo);
    if(right.dd>left.dd+1)return true;
    right.hh+=24*(right.dd-left.dd);
    if(right.hh>left.hh+1)return true;
    right.mi+=60*(right.hh-left.hh);
    right.ss+=60*(right.mi-left.mi);
    if(right.ss-left.ss>=3600)return true;
    return false;
}
int main(void)
{
	int T;
	cin>>T;
    for (int k=1; k<=T; k++) 
	{
        bool flag=false;
        memset(num_of_task, 0, sizeof(num_of_task));
        memset(per_start, 0, sizeof(per_start));
        memset(per_end, 0, sizeof(per_end));
        memset(t, 0, sizeof(t));
        num_of_time = 0;
        t[++num_of_time] = start_time;		
        t[++num_of_time] = end_time; 		
        cin>>per_num;
        for (int i = 1; i <=per_num; i++) 
		{
            cin >> num_of_task[i];
            for (int j = 1; j <= num_of_task[i]; j++) 
			{
                cin >> per_start[i][j].yy>> per_start[i][j].mo >> per_start[i][j].dd >> per_start[i][j].hh >> per_start[i][j].mi >> per_start[i][j].ss;
                cin >> per_end[i][j].yy >> per_end[i][j].mo >> per_end[i][j].dd >> per_end[i][j].hh >> per_end[i][j].mi >> per_end[i][j].ss;
                t[++num_of_time] = per_start[i][j];
                t[++num_of_time] = per_end[i][j];
                string str;
				getline(cin,str);
            }
        }
        sort(t+1, t+1+num_of_time);
        cout<<"Scenario #"<<k<<":\n";
        int l=1,r=1;
        while (l<=num_of_time&&r<=num_of_time) 
		{
            r++;
            if(r>num_of_time)break;
            while (r<=num_of_time&&judge_end(r)) {
                r++;
            }
            r--;		
            if (able_1h(l,r))
			{
      	     	flag = true;
      	      	printf("appointment possible from ");
      	     	t[l].output();
      		  	printf(" to ");
        	    t[r].output();
        	    printf("\n");
        	}
        	l=r+1;		
            while (l<=num_of_time&&!judge_start(l)) 	
			{
           	    l++;
           	}
			if(l>num_of_time)
				break;		
            r=l;		
        }
        if(!flag)cout<<"no appointment possible\n";
        cout<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值