Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <cstring>
- #include <algorithm>
- #include <assert.h>
- #define MAXINT 2147483647
- typedef long long ll;
- class Vector {
- private:
- ll *payload;
- size_t capacity;
- protected:
- size_t n;
- public:
- Vector(size_t _capacity = 1) : n(0), capacity(_capacity) {
- payload = new ll[capacity];
- }
- ll& at(size_t i) {
- if (i >= n) {
- throw "Out of bound";
- }
- return payload[i];
- }
- virtual void push_back(const ll& el) {
- if (n >= capacity) {
- resize(capacity * 2);
- }
- payload[n++] = el;
- }
- void resize(size_t _capacity) {
- ll *prev_payload = payload;
- capacity = _capacity;
- payload = new ll[capacity];
- memcpy(payload, prev_payload, n * sizeof(ll));
- delete prev_payload;
- }
- size_t size() {
- return n;
- }
- bool empty() {
- return n == 0;
- }
- };
- class Stack : public Vector {
- public:
- virtual void pop_back() {
- if (n == 0) {
- throw "Out of bound";
- }
- --n;
- }
- ll& back() {
- if (n == 0) {
- throw "Out of bound";
- }
- return at(n - 1);
- }
- };
- class StackWithMaximum : public Stack {
- private:
- Stack maxStack;
- public:
- void push_back(const ll& el) {
- if (empty()) {
- maxStack.push_back(el);
- } else {
- maxStack.push_back(std::max(el, maxStack.back()));
- }
- Stack::push_back(el);
- }
- void pop_back() {
- maxStack.pop_back();
- Stack::pop_back();
- }
- ll max() {
- return maxStack.back();
- }
- };
- class QueueWithMaximum {
- private:
- StackWithMaximum producerStack;
- StackWithMaximum consumerStack;
- void consume() {
- while (!consumerStack.empty()) {
- ll last = consumerStack.back();
- consumerStack.pop_back();
- producerStack.push_back(last);
- }
- }
- public:
- void push_back(const ll& el) {
- consumerStack.push_back(el);
- }
- void pop_front() {
- if (producerStack.empty()) {
- consume();
- }
- producerStack.pop_back();
- }
- ll& front() {
- if (producerStack.empty()) {
- consume();
- }
- return producerStack.back();
- }
- ll max() {
- if (producerStack.empty()) {
- return consumerStack.max();
- } else if (consumerStack.empty()) {
- return producerStack.max();
- } else {
- return std::max(producerStack.max(), consumerStack.max());
- }
- }
- size_t size() {
- return producerStack.size() + consumerStack.size();
- }
- };
- void test() {
- QueueWithMaximum qm;
- qm.push_back(1);
- assert(qm.max() == 1);
- qm.push_back(5);
- qm.push_back(4);
- assert(qm.max() == 5);
- qm.push_back(3);
- qm.push_back(3);
- assert(qm.max() == 5);
- qm.pop_front();
- assert(qm.max() == 5);
- qm.pop_front();
- assert(qm.max() == 4);
- qm.pop_front();
- assert(qm.max() == 3);
- qm.pop_front();
- assert(qm.max() == 3);
- }
- int main() {
- test();
- ll count, width;
- ll currentHeight, currentArea = 0;
- ll minimumAdditionalArea = MAXINT;
- QueueWithMaximum fenceHeights;
- std::cin >> count >> width;
- for (int i = 0; i < count; ++i) {
- std::cin >> currentHeight;
- fenceHeights.push_back(currentHeight);
- currentArea += currentHeight;
- if (fenceHeights.size() == width) {
- ll additionalArea = fenceHeights.max() * width - currentArea;
- if (additionalArea < minimumAdditionalArea) {
- minimumAdditionalArea = additionalArea;
- }
- currentArea -= fenceHeights.front();
- fenceHeights.pop_front();
- }
- }
- std::cout << minimumAdditionalArea << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement