#include <iostream> #include <array> #include <deque> #include <algorithm> struct oven { int time; int id; int result; oven() {} oven(int time, int id) : time(time), id(id) { result = 0; } static bool lower_time(const oven a, const oven b) { return a.time > b.time; } static bool upper_id(const oven a, const oven b) { return a.id < b.id; } }; struct service { int quant; int time; service() {} service(int quant, int time) : quant(quant), time(time) {} }; int end_of_service(int quant, int time, int time_in_oven, int possible_start_service) { return time + std::max(0, time_in_oven - (time - possible_start_service)) + ((quant - 1) * time_in_oven); } int waiting(int quant, int time, int time_in_oven, int possible_start_service) { return ((2 * std::max(0, time_in_oven - (time - possible_start_service)) + (quant - 1) * time_in_oven) * quant) / 2; } std::deque<service> services; std::array<oven, 200005> ovens; std::array<int, 200005> costumers; int n, m; void read() { std::cin >> n >> m; for (int i = 0; i < n; i++) std::cin >> costumers[i]; std::sort(std::begin(costumers), std::begin(costumers) + n); costumers[n] = costumers[n - 1] + 1; //std::cout << costumers[0] << " "; for (int i = 1; i <= n; i++) { int temp = 1; if (costumers[i] != costumers[i - 1]) { services.push_back(service(temp, costumers[i - 1])); temp = 1; } } //std::cout << std::endl; for (int i = 0; i < m; i++) { int t; std::cin >> t; ovens[i] = oven(t, i); } std::sort(std::begin(ovens), std::end(ovens), oven::lower_time); } void solve() { for (int i = 0; i < m; i++) { int begin = 0; int waiting_time = 0; for (auto j = services.begin(); j != services.end(); ++j) { int t = waiting((*j).quant, (*j).time, ovens[i].time, begin); waiting_time += t; //std::cout << t << " " << (*j).time << " " << (*j).quant << std::endl; begin = end_of_service((*j).quant, (*j).time, ovens[i].time, begin); } //std::cout << std::endl << std::endl; ovens[i].result = waiting_time; } /*for (int i = 0; i < m; i++) { std::cout << ovens[i].result << " " << ovens[i].id << std::endl; }*/ std::sort(std::begin(ovens), std::begin(ovens) + m, oven::upper_id); } void write() { for (int i = 0; i < m; i++) { std::cout << ovens[i].result << std::endl; } } int main() { read(); solve(); write(); 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 | #include <iostream> #include <array> #include <deque> #include <algorithm> struct oven { int time; int id; int result; oven() {} oven(int time, int id) : time(time), id(id) { result = 0; } static bool lower_time(const oven a, const oven b) { return a.time > b.time; } static bool upper_id(const oven a, const oven b) { return a.id < b.id; } }; struct service { int quant; int time; service() {} service(int quant, int time) : quant(quant), time(time) {} }; int end_of_service(int quant, int time, int time_in_oven, int possible_start_service) { return time + std::max(0, time_in_oven - (time - possible_start_service)) + ((quant - 1) * time_in_oven); } int waiting(int quant, int time, int time_in_oven, int possible_start_service) { return ((2 * std::max(0, time_in_oven - (time - possible_start_service)) + (quant - 1) * time_in_oven) * quant) / 2; } std::deque<service> services; std::array<oven, 200005> ovens; std::array<int, 200005> costumers; int n, m; void read() { std::cin >> n >> m; for (int i = 0; i < n; i++) std::cin >> costumers[i]; std::sort(std::begin(costumers), std::begin(costumers) + n); costumers[n] = costumers[n - 1] + 1; //std::cout << costumers[0] << " "; for (int i = 1; i <= n; i++) { int temp = 1; if (costumers[i] != costumers[i - 1]) { services.push_back(service(temp, costumers[i - 1])); temp = 1; } } //std::cout << std::endl; for (int i = 0; i < m; i++) { int t; std::cin >> t; ovens[i] = oven(t, i); } std::sort(std::begin(ovens), std::end(ovens), oven::lower_time); } void solve() { for (int i = 0; i < m; i++) { int begin = 0; int waiting_time = 0; for (auto j = services.begin(); j != services.end(); ++j) { int t = waiting((*j).quant, (*j).time, ovens[i].time, begin); waiting_time += t; //std::cout << t << " " << (*j).time << " " << (*j).quant << std::endl; begin = end_of_service((*j).quant, (*j).time, ovens[i].time, begin); } //std::cout << std::endl << std::endl; ovens[i].result = waiting_time; } /*for (int i = 0; i < m; i++) { std::cout << ovens[i].result << " " << ovens[i].id << std::endl; }*/ std::sort(std::begin(ovens), std::begin(ovens) + m, oven::upper_id); } void write() { for (int i = 0; i < m; i++) { std::cout << ovens[i].result << std::endl; } } int main() { read(); solve(); write(); return 0; } |