Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <bitset>
- #include <iomanip>
- #include <math.h>
- #include <stdlib.h>
- #include <map>
- #include <unordered_map>
- #include <tuple>
- #include <cmath>
- #include <functional>
- #include <algorithm>
- #include <set>
- #include <unordered_set>
- #include <deque>
- #include <numeric>
- #include <cstring>
- struct v {
- int l;
- int r;
- std::string name;
- int type;
- };
- const std::vector<int> def_year = {30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- const std::vector<int> spc_year = {30, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- std::vector<v> events;
- bool cmp(const v& v1, const v& v2) {
- if (v1.l < v2.l)
- return true;
- else if (v1.l == v2.l && v1.r < v2.r)
- return true;
- else if (v1.l == v2.l && v1.r == v2.r && v1.type > v2.type)
- return true;
- else
- return false;
- }
- bool operator<(const v& v1, const v& v2) {
- return cmp(v1, v2);
- }
- int date_to_num(std::string s) {
- int days = (s[0] - 48) * 10 + (s[1] - 48);
- int months = (s[3] - 48) * 10 + (s[4] - 48);
- int years = (s[6] - 48) * 1000 + (s[7] - 48) * 100 + (s[8] - 48) * 10 + (s[9] - 48);
- int number = 0;
- int cur_year = 1500;
- while (cur_year < years) {
- if ((cur_year % 4 == 0 && cur_year % 100 != 0) || cur_year % 400 == 0)
- number += 366;
- else
- number += 365;
- cur_year++;
- }
- if ((cur_year % 4 == 0 && cur_year % 100 != 0) || cur_year % 400 == 0) {
- for (int i = 0; i < months - 1; i++)
- number += spc_year[i];
- } else {
- for (int i = 0; i < months - 1; i++)
- number += def_year[i];
- }
- number += days;
- return number;
- }
- void num_to_date(int number) {
- int year = 1500;
- int months = 1;
- while (true) {
- if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
- if (number > 366)
- number -= 366;
- else
- break;
- else
- if (number > 365)
- number -= 365;
- else
- break;
- year++;
- }
- if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
- for (int i = 0; i < 12; i++) {
- if (number > spc_year[i])
- months++;
- else
- break;
- }
- else
- for (int i = 0; i < 12; i++) {
- if (number > def_year[i]) {
- months++;
- number -= def_year[i];
- } else
- break;
- }
- int days = number + 1;
- if (days < 10)
- std::cout << '0' << days << '.';
- else
- std::cout << days << '.';
- if (months < 10)
- std::cout << '0' << months << '.';
- else
- std::cout << months << '.';
- std::cout << year;
- }
- bool check(int M, int n) {
- std::unordered_set<std::string> names;
- int i = M;
- int j = 0;
- std::set<v> queue;
- while (j < events.size()) {
- while (j < events.size() && events[j].l <= i) {
- if (events[j].type == 0)
- queue.insert(events[j]);
- else if (events[j].type == 1)
- if (names.find(events[j].name) == names.end())
- return false;
- j++;
- }
- if (queue.size() != 0) {
- names.insert((*queue.begin()).name);
- queue.erase(*queue.begin());
- }
- i++;
- }
- return true;
- }
- int main() {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- std::ios::sync_with_stdio(false);
- std::cin.tie(0);
- std::cout.tie(0);
- int n;
- std::cin >> n;
- std::string name, date;
- int t;
- int min_L = (int) 1e9, min_R = (int) 1e9;
- for (int i = 0; i < n; i++) {
- std::cin >> name >> date >> t;
- int num = date_to_num(date);
- events.push_back({num - t, num, name, 0});
- events.push_back({num, num - t, name, 1});
- min_L = std::min(min_L, num - t);
- min_R = std::min(min_R, num - 1);
- }
- std::sort(events.begin(), events.end(), cmp);
- int L = min_L, R = min_R;
- while (R - L > 1) {
- int M = (L + R) / 2;
- if (check(M, n))
- L = M;
- else
- R = M;
- }
- if (check(L, n)) {
- //std::cout << L << '\n';
- num_to_date(L);
- } else
- std::cout << "Impossible";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement