Week10—CSP模拟—团队聚会

题目描述:

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组数据。

接下来对于所有可以用来开会的时间段,每一个时间段输出一行。

需要满足如下规则:

  1. 在该时间段的任何时间点,都应该有至少两人在场。
  2. 在该时间段的任何时间点,至多有一位成员缺席。
  3. 该时间段的时间长度至少应该1h。
  4. 所有的成员都乐意一天24h进行工作。

举个例子,假如现在TA团队有3位成员,TT、zjm、hrz。

那么这样的时间段是合法的:会议开始之初只有TT和zjm,后来hrz加入了,hrz加入之后TT离开了,此后直到会议结束,hrz和zjm一直在场。

要求:

  1. 输出满足条件的所有的时间段,尽管某一段可能有400年那么长。
  2. 时间点的格式为MM/DD/YYYY hh:mm:ss。
  3. 时间段的输出格式为"appointment possible from T0 to T1",其中T0和T1均应满足时间点的格式。
  4. 严格按照格式进行匹配,如果长度不够则在前面补前导0。
  5. 按时间的先后顺序输出各个时间段。
  6. 如果没有合适的时间段,输出一行"no appointment possible"。
  7. 每组数据末尾须打印额外的一行空行。

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

个人思路:

  1. 该题的关键是对时间结构体的设计以及符号的重载:
struct TimeNode {
    int year, month, day;
    int hour, minute, second;

    TimeNode() {}

    TimeNode(int a, int b, int c, int d, int e, int f) {
        year = a;
        month = b;
        day = c;
        hour = d;
        minute = e;
        second = f;
    }

    TimeNode(int *a) {
        year = a[0];
        month = a[1];
        day = a[2];
        hour = a[3];
        minute = a[4];
        second = a[5];
    }

    TimeNode operator=(const TimeNode &b) {
        year = b.year;
        month = b.month;
        day = b.day;
        hour = b.hour;
        minute = b.minute;
        second = b.second;
    }

    bool operator<(const TimeNode &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 (minute != b.minute)return minute < b.minute;
        return second < b.second;
    }

    bool operator>(const TimeNode &b) const { return b < *this; }

    bool operator<=(const TimeNode &b) const { return !(b < *this); }

    bool operator>=(const TimeNode &b) const { return !(*this < b); }

    bool operator==(const TimeNode &b) const { return !(b < *this || *this < b); }

    bool operator!=(const TimeNode &b) const { return b < *this || *this < b; }

    void Output() {
        cout << setw(2) << setfill('0') << month << '/';
        cout << setw(2) << setfill('0') << day << '/';
        cout << setw(4) << setfill('0') << year << ' ';
        cout << setw(2) << setfill('0') << hour << ':';
        cout << setw(2) << setfill('0') << minute << ':';
        cout << setw(2) << setfill('0') << second;

    }
};
  1. 除此之外,对时间的处理以及合理性的判断相对就比较简单了,确定空闲时间段的左右界,对于给定的时间点要确定每个成员在该点的状态,统计在该时间点合法的成员总数,确保大于等于2人且最多缺席1人。

代码块:

#include <iostream>
#include <iomanip>
#include <cstring>
#include <vector>
#include <set>

using namespace std;

struct TimeNode {
    int year, month, day;
    int hour, minute, second;

    TimeNode() {}

    TimeNode(int a, int b, int c, int d, int e, int f) {
        year = a;
        month = b;
        day = c;
        hour = d;
        minute = e;
        second = f;
    }

    TimeNode(int *a) {
        year = a[0];
        month = a[1];
        day = a[2];
        hour = a[3];
        minute = a[4];
        second = a[5];
    }

    TimeNode operator=(const TimeNode &b) {
        year = b.year;
        month = b.month;
        day = b.day;
        hour = b.hour;
        minute = b.minute;
        second = b.second;
    }

    bool operator<(const TimeNode &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 (minute != b.minute)return minute < b.minute;
        return second < b.second;
    }

    bool operator>(const TimeNode &b) const { return b < *this; }

    bool operator<=(const TimeNode &b) const { return !(b < *this); }

    bool operator>=(const TimeNode &b) const { return !(*this < b); }

    bool operator==(const TimeNode &b) const { return !(b < *this || *this < b); }

    bool operator!=(const TimeNode &b) const { return b < *this || *this < b; }

    void Output() {
        cout << setw(2) << setfill('0') << month << '/';
        cout << setw(2) << setfill('0') << day << '/';
        cout << setw(4) << setfill('0') << year << ' ';
        cout << setw(2) << setfill('0') << hour << ':';
        cout << setw(2) << setfill('0') << minute << ':';
        cout << setw(2) << setfill('0') << second;

    }
};

int n;
set<TimeNode> s;
vector<TimeNode> Node;
vector<pair<TimeNode, TimeNode> > A[22], Ans;

void clr(int n) {
    s.clear();
    Ans.clear();
    Node.clear();
    for (int i = 0; i <= n; ++i)A[i].clear();
}

bool checkPer(int id, TimeNode l, TimeNode r) {
    for (int i = 0; i < A[id].size(); ++i) {
        if (l >= A[id][i].first && r <= A[id][i].second)return 0;
    }
    return 1;
}

bool check(TimeNode l, TimeNode r) {
    int tot = 0;
    for (int i = 1; i <= n; ++i) {
        if (checkPer(i, l, r))++tot;
    }
    return (tot >= n - 1 && tot >= 2);
}

bool checkLen(TimeNode l, TimeNode r) {
    if (r.year - l.year >= 2)return 1;
    r.month += (r.year - l.year) * 12;
    if (r.month - l.month >= 2)return 1;
    r.day += (r.month - l.month) * 30;
    if (r.day - l.day >= 2)return 1;
    r.hour += (r.day - l.day) * 24;
    if (r.hour - l.hour >= 2)return 1;
    r.minute += (r.hour - l.hour) * 60;
    r.second += (r.minute - l.minute) * 60;
    if (r.second - l.second >= 3600)return 1;
    return 0;
}

int main() {
    ios::sync_with_stdio(0);
    TimeNode Begin(1800, 1, 1, 0, 0, 0), End(2200, 1, 1, 0, 0, 0);
    int _;
    cin >> _;
    for (int sce = 1; sce <= _; ++sce) {
        cin >> n;
        clr(n);
        s.insert(Begin);
        s.insert(End);
        for (int i = 1; i <= n; ++i) {
            int m;
            cin >> m;
            for (int j = 1; j <= m; ++j) {
                int a[10], b[10];
                for (int k = 0; k < 6; ++k)cin >> a[k];
                for (int k = 0; k < 6; ++k)cin >> b[k];
                string str;  //没用
                getline(cin, str);
                TimeNode l(a), r(b);
                A[i].push_back(make_pair(l, r));
                s.insert(l);
                s.insert(r);
            }
        }
        for (set<TimeNode>::iterator it = s.begin(); it != s.end(); ++it) {
            Node.push_back(*it);
        }

        int l = 0, r = 0, cnt = Node.size();
        cout << "Scenario #" << sce << ":\n";
        while (l < cnt && r < cnt) {
            if (r >= cnt) break;
            while (r < cnt - 1 && check(Node[r], Node[r + 1]))++r;
            if (checkLen(Node[l], Node[r]))Ans.push_back(make_pair(Node[l], Node[r]));
            l = r + 1;
            while (l < cnt - 1 && !check(Node[l], Node[l + 1]))++l;
            r = l;
        }
        if (Ans.size() == 0)cout << "no appointment possible\n";
        else {
            for (int i = 0; i < Ans.size(); ++i) {
                cout << "appointment possible from ";
                Ans[i].first.Output();
                cout << " to ";
                Ans[i].second.Output();
                cout << '\n';
            }
        }
        cout << endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值