#include <cstdio> #include <cstdint> #include <algorithm> #include <iostream> typedef int64_t int64; // Last { // // void update(int64 customer) { // if (last + oven > customer) { // last = last + oven; // } else { // last = customer; // } // } // } int64 totalCost = 0; int64 costPerUnit = 0; class Node { public: int64 qIdx; int64 numActive; int64 customer; Node* prev; Node* next; int64 getStart() { return prev == nullptr ? 0 : prev->customer; } int64 getActivationPoint() { //printf("getActivationPoint64 %d %d \n", customer, prev->customer); return (customer - prev->getStart()) / (prev->numActive + 1) + 1; } int64 getCostPerUnit() { return (int64)numActive * (numActive + 1) / 2; } void merge(Node* other) { if (other != nullptr && other->numActive > 0) { //printf("Merging customer %d with %d.\n", customer, other->customer); this->numActive += other->numActive; if (other == next) { this->next = other->next; if (this->next) { this->next->prev = this; } } else { this->customer = other->customer; this->prev = other->prev; if (this->prev) { this->prev->next = this; } } //delete other; } } void activate(int64 oven) { //printf("ACTIVATING customer %d at oven size %d.\n", customer, oven); int64 delay = 0; if (prev == nullptr || prev->numActive == 0) { delay = getStart() + oven - customer; } else { delay = prev->getStart() + (prev->numActive + 1) * oven - customer; } totalCost += delay; //printf("Updated cost to %ld\n", totalCost); this->numActive = 1; if (next != nullptr) { totalCost += delay * next->numActive; costPerUnit -= next->getCostPerUnit(); merge(next); } if (prev != nullptr) { costPerUnit -= prev->getCostPerUnit(); merge(prev); } costPerUnit += this->getCostPerUnit(); } }; struct Oven { int64 size; int64 originalIdx; int64 cost; }; std::vector<Oven> ovens; typedef std::pair<Node*, int> ScoredNode; class Q { public: std::vector<ScoredNode> nodes; ScoredNode pop() { ScoredNode result = nodes[0]; nodes[0] = nodes.back(); nodes[0].first->qIdx = 0; nodes.pop_back(); popDown(0); return result; } void popDown(int64 idx) { int64 children = nodes.size() - idx * 2 - 1; if (children <= 0) { return; } int64 best = idx * 2 + 1; if (children > 1 && nodes[best + 1].second < nodes[best].second) { best++; } ScoredNode node = nodes[idx]; if (node.second > nodes[best].second) { nodes[idx] = nodes[best]; nodes[best] = node; nodes[idx].first->qIdx = idx; nodes[best].first->qIdx = best; popDown(best); } } void popUp(int64 idx) { ScoredNode node = nodes[idx]; int64 parentIdx = (idx - 1) / 2; if (idx > 0 && node.second < nodes[parentIdx].second) { nodes[idx] = nodes[parentIdx]; nodes[parentIdx] = node; nodes[idx].first->qIdx = idx; nodes[parentIdx].first->qIdx = parentIdx; popUp(parentIdx); } } void iterate() { int64 ovenIdx = 0; int64 prevSize = 0; while (nodes.size() > 0) { ScoredNode scoredNode = pop(); int64 currentSize = scoredNode.second; while (ovens[ovenIdx].size < currentSize) { ovens[ovenIdx].cost = totalCost + costPerUnit * (ovens[ovenIdx].size - prevSize); if (++ovenIdx == ovens.size()) { return; } } totalCost += costPerUnit * (currentSize - prevSize); prevSize = scoredNode.second; Node* node = scoredNode.first; node->activate(scoredNode.second); // printf("Cost at %d is %d, %d\n", scoredNode.second, totalCost, // costPerUnit); Node* next = node->next; if (next != nullptr) { nodes[next->qIdx].second = next->getActivationPoint(); if (nodes[next->qIdx].first != next) { printf("MISMATCH!!!!"); } // printf("Updating act pt customer %d at to %d.\n", // next->customer, nodes[next->qIdx].second); popUp(next->qIdx); } } for (; ovenIdx < ovens.size(); ovenIdx++) { ovens[ovenIdx].cost = totalCost + (ovens[ovenIdx].size - prevSize) * costPerUnit; } } void init(const std::vector<int64>& customers) { int64 last = 0; Node* prev = nullptr; for (int64 c : customers) { Node* node = new Node(); node->numActive = 0; node->customer = c; node->prev = prev; node->next = nullptr; if (prev != nullptr) { prev->next = node; } node->qIdx = nodes.size(); nodes.push_back({node, c - last + 1}); popUp(nodes.size() - 1); last = c; prev = node; } } void solve(const std::vector<int64>& customers) { init(customers); iterate(); } }; int main(int argc, char **argv) { std::ios_base::sync_with_stdio(false); int64 customersCount, ovensCount; std::cin >> customersCount >> ovensCount; std::vector<int64> customers(customersCount); //std::vector<int> ovens(ovensCount); for (int64 i = 0; i < customersCount; i++) { std::cin >> customers[i]; } ovens.resize(ovensCount); for (int64 i = 0; i < ovensCount; i++) { std::cin >> ovens[i].size; ovens[i].originalIdx = i; } if (true) { sort(ovens.begin(),ovens.end(), [](const Oven &x, const Oven &y){ return (x.size < y.size);}); Q q; q.solve(customers); sort(ovens.begin(),ovens.end(), [](const Oven &x, const Oven &y){ return (x.originalIdx < y.originalIdx);}); for (const auto& oven : ovens) { std::cout << oven.cost << "\n"; } return 0; } // for (const auto& oven : ovens) { // int64 last = 0; // int64 result = 0; // for (int64 customer : customers) { // int64 penalty = std::max((int64)0, last + oven.size - customer); // last = customer + penalty; // result += penalty; // } // std::cout << result << "\n"; // } 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | #include <cstdio> #include <cstdint> #include <algorithm> #include <iostream> typedef int64_t int64; // Last { // // void update(int64 customer) { // if (last + oven > customer) { // last = last + oven; // } else { // last = customer; // } // } // } int64 totalCost = 0; int64 costPerUnit = 0; class Node { public: int64 qIdx; int64 numActive; int64 customer; Node* prev; Node* next; int64 getStart() { return prev == nullptr ? 0 : prev->customer; } int64 getActivationPoint() { //printf("getActivationPoint64 %d %d \n", customer, prev->customer); return (customer - prev->getStart()) / (prev->numActive + 1) + 1; } int64 getCostPerUnit() { return (int64)numActive * (numActive + 1) / 2; } void merge(Node* other) { if (other != nullptr && other->numActive > 0) { //printf("Merging customer %d with %d.\n", customer, other->customer); this->numActive += other->numActive; if (other == next) { this->next = other->next; if (this->next) { this->next->prev = this; } } else { this->customer = other->customer; this->prev = other->prev; if (this->prev) { this->prev->next = this; } } //delete other; } } void activate(int64 oven) { //printf("ACTIVATING customer %d at oven size %d.\n", customer, oven); int64 delay = 0; if (prev == nullptr || prev->numActive == 0) { delay = getStart() + oven - customer; } else { delay = prev->getStart() + (prev->numActive + 1) * oven - customer; } totalCost += delay; //printf("Updated cost to %ld\n", totalCost); this->numActive = 1; if (next != nullptr) { totalCost += delay * next->numActive; costPerUnit -= next->getCostPerUnit(); merge(next); } if (prev != nullptr) { costPerUnit -= prev->getCostPerUnit(); merge(prev); } costPerUnit += this->getCostPerUnit(); } }; struct Oven { int64 size; int64 originalIdx; int64 cost; }; std::vector<Oven> ovens; typedef std::pair<Node*, int> ScoredNode; class Q { public: std::vector<ScoredNode> nodes; ScoredNode pop() { ScoredNode result = nodes[0]; nodes[0] = nodes.back(); nodes[0].first->qIdx = 0; nodes.pop_back(); popDown(0); return result; } void popDown(int64 idx) { int64 children = nodes.size() - idx * 2 - 1; if (children <= 0) { return; } int64 best = idx * 2 + 1; if (children > 1 && nodes[best + 1].second < nodes[best].second) { best++; } ScoredNode node = nodes[idx]; if (node.second > nodes[best].second) { nodes[idx] = nodes[best]; nodes[best] = node; nodes[idx].first->qIdx = idx; nodes[best].first->qIdx = best; popDown(best); } } void popUp(int64 idx) { ScoredNode node = nodes[idx]; int64 parentIdx = (idx - 1) / 2; if (idx > 0 && node.second < nodes[parentIdx].second) { nodes[idx] = nodes[parentIdx]; nodes[parentIdx] = node; nodes[idx].first->qIdx = idx; nodes[parentIdx].first->qIdx = parentIdx; popUp(parentIdx); } } void iterate() { int64 ovenIdx = 0; int64 prevSize = 0; while (nodes.size() > 0) { ScoredNode scoredNode = pop(); int64 currentSize = scoredNode.second; while (ovens[ovenIdx].size < currentSize) { ovens[ovenIdx].cost = totalCost + costPerUnit * (ovens[ovenIdx].size - prevSize); if (++ovenIdx == ovens.size()) { return; } } totalCost += costPerUnit * (currentSize - prevSize); prevSize = scoredNode.second; Node* node = scoredNode.first; node->activate(scoredNode.second); // printf("Cost at %d is %d, %d\n", scoredNode.second, totalCost, // costPerUnit); Node* next = node->next; if (next != nullptr) { nodes[next->qIdx].second = next->getActivationPoint(); if (nodes[next->qIdx].first != next) { printf("MISMATCH!!!!"); } // printf("Updating act pt customer %d at to %d.\n", // next->customer, nodes[next->qIdx].second); popUp(next->qIdx); } } for (; ovenIdx < ovens.size(); ovenIdx++) { ovens[ovenIdx].cost = totalCost + (ovens[ovenIdx].size - prevSize) * costPerUnit; } } void init(const std::vector<int64>& customers) { int64 last = 0; Node* prev = nullptr; for (int64 c : customers) { Node* node = new Node(); node->numActive = 0; node->customer = c; node->prev = prev; node->next = nullptr; if (prev != nullptr) { prev->next = node; } node->qIdx = nodes.size(); nodes.push_back({node, c - last + 1}); popUp(nodes.size() - 1); last = c; prev = node; } } void solve(const std::vector<int64>& customers) { init(customers); iterate(); } }; int main(int argc, char **argv) { std::ios_base::sync_with_stdio(false); int64 customersCount, ovensCount; std::cin >> customersCount >> ovensCount; std::vector<int64> customers(customersCount); //std::vector<int> ovens(ovensCount); for (int64 i = 0; i < customersCount; i++) { std::cin >> customers[i]; } ovens.resize(ovensCount); for (int64 i = 0; i < ovensCount; i++) { std::cin >> ovens[i].size; ovens[i].originalIdx = i; } if (true) { sort(ovens.begin(),ovens.end(), [](const Oven &x, const Oven &y){ return (x.size < y.size);}); Q q; q.solve(customers); sort(ovens.begin(),ovens.end(), [](const Oven &x, const Oven &y){ return (x.originalIdx < y.originalIdx);}); for (const auto& oven : ovens) { std::cout << oven.cost << "\n"; } return 0; } // for (const auto& oven : ovens) { // int64 last = 0; // int64 result = 0; // for (int64 customer : customers) { // int64 penalty = std::max((int64)0, last + oven.size - customer); // last = customer + penalty; // result += penalty; // } // std::cout << result << "\n"; // } return 0; } |