poj1960 Time Planner

题目来源:http://poj.org/problem?id=1960

 

Time Planner
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 457 Accepted: 104

 

Description

Background 
Problems in computer science are often classified as belonging to a certain class of problems (e.g. NP, NP complete, unsolvable, . . . ). Whatever class of problems you have to solve in a team, there is always one main problem: To find a date where all the programmers can meet to work together on their project. 
Problem 
Your task is to write a program that finds all possible appointments for a programming team that is busy all the time and therefore has problems in finding accurate dates and times for their meetings.

Input

The first line of the input contains the number of scenarios. 
For each scenario, you are first given the number m of team members in a single line, with 2 <= m <= 20.For every team member, there will be a line containing the number n of entries in this members calender (0 <= n <= 100), followed by n lines in the following format: 
YYYY MM DD hh mm ss YYYY MM DD hh mm ss some string here

Each such line contains the year, month, day, hour, minute, and second of both starting and ending time for the appointment, and a string with the description of the appointment. All numbers are zero padded to match the given format, with single blanks in-between. The string at the end might contain blanks and is no longer than 100 characters. The dates are in the range January 1, 1800 midnight to January 1, 2200 midnight. For simplification, you may assume that all months (even February) have 30 days each, and no invalid dates (like January 31) will occur. 
Note that the ending time of a team member's appointment specifies the point in time where this team member is ready to join a meeting.

Output

The output for each scenario begins with a line containing Scenario #i:, where i is the number of the scenario starting at 1. Then print a line for each possible appointments that follows these rules: 
  • at any time during the meeting, at least two team members need to be there 
  • at any time during the meeting, at most one team member is allowed to be absent 
  • the meeting should be at least one hour in length 
  • all team members are willing to work 24 hours a day

For example, if there are three team members A, B and C, the following is a valid meeting: It begins solely with A and B. Later C joins the meeting and before the end, A may leave. 
Always print the longest appointment possible satisfying these conditions, even if it is as long as 400 years. Sort these lines by date and time, and use the following format: 
appointment possible from MM/DD/YYYY hh:mm:ss to MM/DD/YYYY hh:mm:ss 
The numbers indicating month, day, year, hour, minute, and second should be zero padded in order to get the required number of characters. If no appointment is possible, just print a line containing no appointment possible Conclude the output for each scenario with a blank line.

Sample Input

2
3
3
2002 06 28 15 00 00 2002 06 28 18 00 00 TUD Contest Practice Session
2002 06 29 10 00 00 2002 06 29 15 00 00 TUD Contest
2002 11 15 15 00 00 2002 11 17 23 00 00 NWERC Delft
4
2002 06 25 13 30 00 2002 06 25 15 30 00 FIFA World Cup Semifinal I
2002 06 26 13 30 00 2002 06 26 15 30 00 FIFA World Cup Semifinal II
2002 06 29 13 00 00 2002 06 29 15 00 00 FIFA World Cup Third Place
2002 06 30 13 00 00 2002 06 30 15 00 00 FIFA World Cup Final
1
2002 06 01 00 00 00 2002 06 29 18 00 00 Preparation of Problem Set
2
1
1800 01 01 00 00 00 2200 01 01 00 00 00 Solving Problem 8
0

Sample Output

Scenario #1:
appointment possible from 01/01/1800 00:00:00 to 06/25/2002 13:30:00
appointment possible from 06/25/2002 15:30:00 to 06/26/2002 13:30:00
appointment possible from 06/26/2002 15:30:00 to 06/28/2002 15:00:00
appointment possible from 06/28/2002 18:00:00 to 06/29/2002 10:00:00
appointment possible from 06/29/2002 15:00:00 to 01/01/2200 00:00:00

Scenario #2:
no appointment possible

 

最开始做的时候从网上搜不到题解,POJ Disscuss里面也没人讨论这个陈年旧题……做这个题的时候我在想,如果有朝一日AC了这道题,一定要发一份题解……

时间节点这一数据类型用c++里的结构体来表示,为了后面处理起来方便,在结构体内重载了=  <  >  <=  >=  ==  !=这几个运算符(详见代码),尽管有些并没有用到。

思路:

将所有出现的时间节点全部投射到时间轴上,按时间顺序枚举所有的小区间。

对于每个小区间,判断其是否合法。对于前后连续的合法区间,对这些区间进行合并。

详见代码。

 

代码:

#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;
}
/*
 1
 2
 1
 1800 01 01 00 00 00 2200 01 01 00 00 00 sleep
 0
 */

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值