【团队聚会】差分+hash+复杂模拟

题目

POJ-1960 Time Planner

题目描述
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

题目大意

本题给出若干组数据,每组会给出一定人数和这些人在限定时间范围内被占用的时间段,题目要求在限定总时间内找出所有可以开会的时间段,可以开会的时间段必须满足到场人数大于等于二,缺席人数小于等于一且开会时长大于等于一小时,要求列出所有可以开会的时段。

解题思路

本题的输入输出十分复杂,可见其中有模拟题的成分,需要认真完成读入输出过程。认真读题之后可以发现本题实质上就是求每个人空闲的公共区间,但是由于时间输入十分复杂,所以需要用hash的思想现将时间转化为ll类型,这样就便于在连续数轴上操作。此处的hash过程十分复杂,同时还要注意之后在这方面的相关积累。对于空闲时间交集的处理,我们可以转化为忙碌区间处理,每次将忙碌区间头至尾内部所有数加一。最终如果一个区间上的值小于等于一,说明此处只有一人或没人忙绿,此处很明显可以使用差分数列思想。
综上所述,本题涉及内容较多且实现起来复杂繁琐,之后需要多次复习回顾相关知识。

具体代码

#include<iostream>
#include<sstream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<iomanip>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#define MAXN 100005
#define ll long long

using namespace std;

struct time{
	vector<pair<ll,ll> > time;
}a[105];
int tot,num;
map<ll,int> q;
ll h1 = 12,h2 = 30,h3 = 24, h4 = 60;
ll start = (ll)1800*h1*h2*h3*h4*h4;
ll end = (ll)2200*h1*h2*h3*h4*h4;

bool cmp(pair<ll,ll> x,pair<ll,ll> y)
{
    if(x.first != y.first)
	{
		return x.first < y.first;
	}
    return x.second < y.second;
}

void make_time(string s)
{
	ll t1 = 0,t2 = 0;
	int n = 0;
	int flag = 1;
	pair<ll,ll> tmp;
	for(int i = 0; i < 6; i++)
	{
		t2 = 0;
		while(true)
	    {
	        if('0' <= s[n] && s[n] <= '9')
	        {
	            t2 = t2 * 10 + int(s[n] - '0');
	        }
	        if(s[n] == ' ')
	        {
	            if(flag==1){t1=t1+t2*h1*h2*h3*h4*h4;}
	            if(flag==2){t1=t1+(t2-1)*h2*h3*h4*h4;}
	            if(flag==3){t1=t1+(t2-1)*h3*h4*h4;}
	            if(flag==4){t1=t1+t2*h4*h4;}
	            if(flag==5){t1=t1+t2*h4;}
	            if(flag==6){t1=t1+t2;}
	            flag++;
	            n++;
	            break;
	        }
	        n++;
	    }
	}
    tmp.first = t1;
    t1 = 0;
    t2 = 0;
    flag = 1;
    for(int i = 0; i < 6; i++)
	{
		t2 = 0;
		while(true)
	    {
	        if('0' <= s[n] && s[n] <= '9')
	        {
	            t2 = t2 * 10 + int(s[n] - '0');
	        }
	        if(s[n] == ' ')
	        {
	            if(flag==1){t1=t1+t2*h1*h2*h3*h4*h4;}
	            if(flag==2){t1=t1+(t2-1)*h2*h3*h4*h4;}
	            if(flag==3){t1=t1+(t2-1)*h3*h4*h4;}
	            if(flag==4){t1=t1+t2*h4*h4;}
	            if(flag==5){t1=t1+t2*h4;}
	            if(flag==6){t1=t1+t2;}
	            flag++;
	            n++;
	            break;
	        }
	        n++;
	    }
	}
	tmp.second = t1;
	a[tot].time.push_back(tmp);
}

string get_num(ll v)
{
    ll t = h1*h2*h3*h4*h4;
    ll t1[6];
    t1[0] = v / t;
    v = v - t1[0] * t;
    t = h2*h3*h4*h4;
    t1[1] = v / t;
    v = v - t1[1] * t;
    t = h3*h4*h4;
    t1[2] = v / t;
    v = v - t1[2] * t;
    t = h4*h4;
    t1[3] = v / t;
    v = v - t1[3] * t;
    t = h4;
    t1[4] = v / t;
    v = v - t1[4] * t;
    t1[5] = v;
    stringstream s;
    t1[1]++;
    t1[2]++;
    if(t1[1]<10)s<<'0'<<t1[1]<<'/';
    else s<<t1[1]<<'/';
    if(t1[2]<10)s<<'0'<<t1[2]<<'/';
    else s<<t1[2]<<'/';
    s<<t1[0]<<' ';
    if(t1[3]<10)s<<'0'<<t1[3]<<':';
    else s<<t1[3]<<':';
    if(t1[4]<10)s<<'0'<<t1[4]<<':';
    else s<<t1[4]<<':';
    if(t1[5]<10)s<<'0'<<t1[5];
    else s<<t1[5];
    return s.str();
}

void solve(int k)
{
	cout << "Scenario #" << k << ":" << endl;
    int flag = 0;
    q[end] = -1;
    ll sy = 0,last = 0;
    ll minn = start;
    for(map<ll,int>::iterator it = q.begin(); it != q.end(); it++)
    {
        ll t1 = it->first;
		ll t2 = it->second;
        sy += t2;
        if(num - sy >= 2 && sy <= 1)
        {
            minn = min(minn,t1);
            last = t1;
        }
		else
        {
            last = t1;
            if(last - minn >= 3600)
            {
                flag++;
                cout << "appointment possible from " << get_num(minn) << " to " << get_num(last) << endl;
            }
            minn = 1e17;
        }
    }
    last = end;
    if(last - minn >= 3600)
    {
        flag++;
        cout << "appointment possible from " << get_num(minn) << " to " << get_num(last) << endl;
    }
    if(!flag)
    {
        cout << "no appointment possible" << endl;
    }
    cout << endl;
    return;
}

int main()
{
	int t;
	cin >> t;
	for(int k = 1; k <= t; k++)
	{
		q.clear();
		tot = 0;
		cin >> num;
		for(int i = 0; i < num; i++)
		{
			int x;
			cin >> x;
			if(x == 0)
			{
				continue;
			}
			char c = getchar();
			for(int j = 0; j < x; j++)
			{
				string s;
				getline(cin,s);
				make_time(s);
				q[a[tot].time[j].first]++;
				q[a[tot].time[j].second]--;
			}
			tot++;
		}
		solve(k);
		for(int i = 0; i<105; i++)
        {
            a[i].time.clear();
        }
	}
	return 0; 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值