#include <iostream> #include <vector> #include <unordered_map> #include <limits> #define DEBUG(x) #define DEBUG2(x) #define DEBUG3(x) class DestackCounter { public: DestackCounter(int height) : cache_line_length(3), upper_cache_line(height), lower_cache_line(height), cache_line(height) { if (height < 3) { upper_cache_line.resize(3); lower_cache_line.resize(3); cache_line.resize(3); } DEBUG(std::cout << "upper_cache_line.size() = " << upper_cache_line.size() << std::endl;) upper_cache_line[0] = 1; lower_cache_line[0] = 2; lower_cache_line[1] = 2; cache_line[0] = 3; cache_line[1] = 4; cache_line[2] = 3; } int get_removal_cost(int row, int position) { if (row == 0) return 1; if (row == 1) return 2; if (row == cache_line_length) calculate_next_line(); return cache_line[position]; } private: void calculate_next_line() { upper_cache_line = std::move(lower_cache_line); lower_cache_line = std::move(cache_line); cache_line.resize(upper_cache_line.size()); cache_line_length += 1; for (int i = 0; i < cache_line_length; ++i) { cache_line[i] = get_prev(i) - get_prev_prev(i) + 1; DEBUG(std::cout << cache_line[i] << " ";) } DEBUG(std::cout << std::endl;) } int get_prev(int position) { if (position == 0) return lower_cache_line[0]; if (position == cache_line_length - 1) return lower_cache_line[cache_line_length - 1 - 1]; return lower_cache_line[position - 1] + lower_cache_line[position]; } int get_prev_prev(int position) { if (position == 0) return 0; if (position == cache_line_length - 1) return 0; return upper_cache_line[position - 1]; } int cache_line_length; std::vector<int> upper_cache_line; std::vector<int> lower_cache_line; std::vector<int> cache_line; }; int main() { int height = 0; int request_botels = 0; std::cin >> height >> request_botels; DEBUG(std::cout << "height = " << height << " requested_botels = " << request_botels << std::endl;) int best_year = std::numeric_limits<int>::max(); DestackCounter destack_counter(height); for (int r = 0; r < height; ++r) { for (int p = 0; p <= r; ++p) { int year = 0; std::cin >> year; DEBUG2(std::cout << year << " ";) if (r < request_botels) { const int cost = destack_counter.get_removal_cost(r, p); if (cost <= request_botels && year < best_year) { best_year = year; } DEBUG3(std::cout << "(" << r << ", " << p << ") -> best[" << cost << "] = " << year << std::endl;) } } DEBUG2(std::cout << std::endl;) } std::cout << best_year; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | #include <iostream> #include <vector> #include <unordered_map> #include <limits> #define DEBUG(x) #define DEBUG2(x) #define DEBUG3(x) class DestackCounter { public: DestackCounter(int height) : cache_line_length(3), upper_cache_line(height), lower_cache_line(height), cache_line(height) { if (height < 3) { upper_cache_line.resize(3); lower_cache_line.resize(3); cache_line.resize(3); } DEBUG(std::cout << "upper_cache_line.size() = " << upper_cache_line.size() << std::endl;) upper_cache_line[0] = 1; lower_cache_line[0] = 2; lower_cache_line[1] = 2; cache_line[0] = 3; cache_line[1] = 4; cache_line[2] = 3; } int get_removal_cost(int row, int position) { if (row == 0) return 1; if (row == 1) return 2; if (row == cache_line_length) calculate_next_line(); return cache_line[position]; } private: void calculate_next_line() { upper_cache_line = std::move(lower_cache_line); lower_cache_line = std::move(cache_line); cache_line.resize(upper_cache_line.size()); cache_line_length += 1; for (int i = 0; i < cache_line_length; ++i) { cache_line[i] = get_prev(i) - get_prev_prev(i) + 1; DEBUG(std::cout << cache_line[i] << " ";) } DEBUG(std::cout << std::endl;) } int get_prev(int position) { if (position == 0) return lower_cache_line[0]; if (position == cache_line_length - 1) return lower_cache_line[cache_line_length - 1 - 1]; return lower_cache_line[position - 1] + lower_cache_line[position]; } int get_prev_prev(int position) { if (position == 0) return 0; if (position == cache_line_length - 1) return 0; return upper_cache_line[position - 1]; } int cache_line_length; std::vector<int> upper_cache_line; std::vector<int> lower_cache_line; std::vector<int> cache_line; }; int main() { int height = 0; int request_botels = 0; std::cin >> height >> request_botels; DEBUG(std::cout << "height = " << height << " requested_botels = " << request_botels << std::endl;) int best_year = std::numeric_limits<int>::max(); DestackCounter destack_counter(height); for (int r = 0; r < height; ++r) { for (int p = 0; p <= r; ++p) { int year = 0; std::cin >> year; DEBUG2(std::cout << year << " ";) if (r < request_botels) { const int cost = destack_counter.get_removal_cost(r, p); if (cost <= request_botels && year < best_year) { best_year = year; } DEBUG3(std::cout << "(" << r << ", " << p << ") -> best[" << cost << "] = " << year << std::endl;) } } DEBUG2(std::cout << std::endl;) } std::cout << best_year; return 0; } |