Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <assert.h>
  7.  
  8. #define MAXINT 2147483647
  9.  
  10. typedef long long ll;
  11.  
  12. class Vector {
  13.  
  14. private:
  15. ll *payload;
  16. size_t capacity;
  17.  
  18. protected:
  19. size_t n;
  20.  
  21. public:
  22.  
  23. Vector(size_t _capacity = 1) : n(0), capacity(_capacity) {
  24. payload = new ll[capacity];
  25. }
  26.  
  27. ll& at(size_t i) {
  28. if (i >= n) {
  29. throw "Out of bound";
  30. }
  31. return payload[i];
  32. }
  33.  
  34. virtual void push_back(const ll& el) {
  35. if (n >= capacity) {
  36. resize(capacity * 2);
  37. }
  38. payload[n++] = el;
  39. }
  40.  
  41. void resize(size_t _capacity) {
  42. ll *prev_payload = payload;
  43.  
  44. capacity = _capacity;
  45. payload = new ll[capacity];
  46. memcpy(payload, prev_payload, n * sizeof(ll));
  47.  
  48. delete prev_payload;
  49. }
  50.  
  51. size_t size() {
  52. return n;
  53. }
  54.  
  55. bool empty() {
  56. return n == 0;
  57. }
  58. };
  59.  
  60. class Stack : public Vector {
  61.  
  62. public:
  63.  
  64. virtual void pop_back() {
  65. if (n == 0) {
  66. throw "Out of bound";
  67. }
  68. --n;
  69. }
  70.  
  71. ll& back() {
  72. if (n == 0) {
  73. throw "Out of bound";
  74. }
  75. return at(n - 1);
  76. }
  77.  
  78. };
  79.  
  80. class StackWithMaximum : public Stack {
  81.  
  82. private:
  83. Stack maxStack;
  84.  
  85. public:
  86. void push_back(const ll& el) {
  87. if (empty()) {
  88. maxStack.push_back(el);
  89. } else {
  90. maxStack.push_back(std::max(el, maxStack.back()));
  91. }
  92. Stack::push_back(el);
  93. }
  94.  
  95. void pop_back() {
  96. maxStack.pop_back();
  97. Stack::pop_back();
  98. }
  99.  
  100. ll max() {
  101. return maxStack.back();
  102. }
  103.  
  104. };
  105.  
  106. class QueueWithMaximum {
  107.  
  108. private:
  109. StackWithMaximum producerStack;
  110. StackWithMaximum consumerStack;
  111.  
  112. void consume() {
  113. while (!consumerStack.empty()) {
  114. ll last = consumerStack.back();
  115. consumerStack.pop_back();
  116. producerStack.push_back(last);
  117. }
  118. }
  119.  
  120. public:
  121.  
  122. void push_back(const ll& el) {
  123. consumerStack.push_back(el);
  124. }
  125.  
  126. void pop_front() {
  127. if (producerStack.empty()) {
  128. consume();
  129. }
  130. producerStack.pop_back();
  131. }
  132.  
  133. ll& front() {
  134. if (producerStack.empty()) {
  135. consume();
  136. }
  137. return producerStack.back();
  138. }
  139.  
  140. ll max() {
  141. if (producerStack.empty()) {
  142. return consumerStack.max();
  143. } else if (consumerStack.empty()) {
  144. return producerStack.max();
  145. } else {
  146. return std::max(producerStack.max(), consumerStack.max());
  147. }
  148. }
  149.  
  150. size_t size() {
  151. return producerStack.size() + consumerStack.size();
  152. }
  153.  
  154. };
  155.  
  156. void test() {
  157. QueueWithMaximum qm;
  158. qm.push_back(1);
  159. assert(qm.max() == 1);
  160. qm.push_back(5);
  161. qm.push_back(4);
  162. assert(qm.max() == 5);
  163. qm.push_back(3);
  164. qm.push_back(3);
  165. assert(qm.max() == 5);
  166. qm.pop_front();
  167. assert(qm.max() == 5);
  168. qm.pop_front();
  169. assert(qm.max() == 4);
  170. qm.pop_front();
  171. assert(qm.max() == 3);
  172. qm.pop_front();
  173. assert(qm.max() == 3);
  174. }
  175.  
  176. int main() {
  177.  
  178. test();
  179.  
  180. ll count, width;
  181. ll currentHeight, currentArea = 0;
  182. ll minimumAdditionalArea = MAXINT;
  183.  
  184. QueueWithMaximum fenceHeights;
  185.  
  186. std::cin >> count >> width;
  187.  
  188. for (int i = 0; i < count; ++i) {
  189. std::cin >> currentHeight;
  190. fenceHeights.push_back(currentHeight);
  191. currentArea += currentHeight;
  192.  
  193. if (fenceHeights.size() == width) {
  194. ll additionalArea = fenceHeights.max() * width - currentArea;
  195. if (additionalArea < minimumAdditionalArea) {
  196. minimumAdditionalArea = additionalArea;
  197. }
  198. currentArea -= fenceHeights.front();
  199. fenceHeights.pop_front();
  200. }
  201. }
  202.  
  203. std::cout << minimumAdditionalArea << std::endl;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement