Friday, June 19, 2015

UVa 10191 - Longest Nap

// UVa 10191 - Longest Nap
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct par {
	int first;
	int second;
};

bool operator<(par x, par y) {
	return x.first < y.first;
}

string dummy;

int main() {

	int n;
	int d = 0;

	while (cin >> n) {
		d++;

		vector<par> time;

		for (int i = 0; i < n; i++) {

			string t1, t2;
			cin >> t1 >> t2;
			getline(cin, dummy);

			int a = ((t1[0] - '0') * 10 + t1[1] - '0') * 60 + (t1[3] - '0') * 10 + t1[4] - '0';
			int b = ((t2[0] - '0') * 10 + t2[1] - '0') * 60 + (t2[3] - '0') * 10 + t2[4] - '0';

			par p = { a, b };
			time.push_back(p);
		}

		sort(time.begin(), time.end());

		int longestnap = time[0].first - 10 * 60;
		int starts = 10 * 60;

		for (int i = 1; i < n; i++) {
			if (time[i].first - time[i - 1].second > longestnap) {
				longestnap = time[i].first - time[i - 1].second;
				starts = time[i - 1].second;
			}
		}

		if (18 * 60 - time[n - 1].second > longestnap) {
			longestnap = 18 * 60 - time[n - 1].second;
			starts = time[n - 1].second;
		}

		cout << "Day #" << d << ": the longest nap starts at ";
		cout << starts / 60 << ":";
		if (starts % 60 < 10)
			cout << "0";
		cout << starts % 60 << " and will last for ";

		if (longestnap >= 60)
			cout << longestnap / 60 << " hours and ";
		cout << longestnap % 60 << " minutes." << endl;

	}

}

No comments:

Post a Comment