|
发表于 2022-6-25 12:33:27
From FishC Mobile
|
显示全部楼层
- #include <iostream>
- #include <vector>
- #include <utility>
- using namespace std;
- class Time {
- friend ostream& operator<<(ostream& os, const Time&);
- public:
- int h, m;
- Time();
- Time(int, int);
- inline void add(int);
- bool operator<(const Time& time);
- void operator=(const Time& time);
- };
- Time::Time(): h(0), m(0) { }
- Time::Time(int hour, int minute): h(hour), m(minute) { }
- ostream& operator<<(ostream& os, const Time& time) {
- os << time.h << ":" << time.h;
- return os;
- }
- void Time::add(int minute) {
- m += minute;
- h += m / 60;
- m %= 60;
- h %= 24;
- }
- bool Time::operator<(const Time& time) {
- if (h < time.h) return true;
- else if (h > time.h) return false;
- return m < time.m;
- }
- bool operator<(const Time& A, const Time& B) {
- if (A.h < B.h) return true;
- else if (A.h > B.h) return false;
- return A.m < B.m;
- }
- void Time::operator=(const Time& time) {
- h = time.h;
- m = time.m;
- }
- Time max(const Time& A, const Time& B) {
- if (A < B) return B;
- return A;
- }
- Time lastCall(vector<pair<Time, int>> service, bool twoWindows = false) {
- Time last, A, B;
- if (not twoWindows) {
- for (const auto &[T, m]: service) {
- if (last < T) last = T;
- last.add(m);
- }
- }
- else {
- for (const auto &[T, m]: service) {
- if (m <= 20) {
- if (A < T) A = T;
- A.add(m);
- }
- else {
- if (B < T) B = T;
- B.add(m);
- }
- }
- last = max(A, B);
- }
- return last;
- }
- int main(void) {
- vector<pair<Time, int>> service {
- {make_pair(Time(9, 0), 8)},
- {make_pair(Time(9, 0), 3)},
- {make_pair(Time(9, 3), 18)},
- {make_pair(Time(9, 5), 20)},
- {make_pair(Time(9, 6), 38)},
- {make_pair(Time(9, 10), 8)},
- {make_pair(Time(9, 20), 12)},
- {make_pair(Time(9, 24), 15)},
- {make_pair(Time(9, 35), 43)},
- {make_pair(Time(9, 39), 20)},
- {make_pair(Time(9, 40), 12)},
- {make_pair(Time(9, 41), 12)},
- {make_pair(Time(9, 45), 4)},
- {make_pair(Time(9, 45), 28)},
- };
-
- cout << lastCall(service) << endl;
- cout << lastCall(service, true) << endl;
-
- return 0;
- }
复制代码 |
|