#include <iostream> #include <vector> #include <string> #include <algorithm> #include <deque> #include <stack> #include <iterator> #include <numeric> #include <tuple> #include <set> #include <functional> #include <map> typedef unsigned long long ULL; typedef long long LL; struct Oven { LL cost; int idx; ULL minimalWaitingTime; }; Oven createOven(int idx, LL cost) { Oven o; o.idx = idx; o.cost = cost; o.minimalWaitingTime = 0; return o; } bool cmpByCost(const Oven& lhs, const Oven& rhs) { return lhs.cost < rhs.cost; } bool cmpByIdx(const Oven& lhs, const Oven& rhs) { return lhs.idx < rhs.idx; } int main(int argc, char **argv) { int numberOfCustomers, numberOfOvens; std::cin>>numberOfCustomers>>numberOfOvens; std::vector<ULL> times; std::vector<Oven> ovens; times.reserve(numberOfCustomers); ovens.reserve(numberOfOvens); for(int i = 0; i < numberOfCustomers; ++i) { ULL t; std::cin>>t; times.push_back(t); } for(int i = 0; i < numberOfOvens; ++i) { LL t; std::cin>>t; ovens.push_back(createOven(i, t)); } std::vector<ULL> distances; distances.push_back(times[0]); for(auto i = 1UL; i < times.size(); ++i) { distances.push_back(times[i] - times[i - 1]); } // std::sort(distances.begin(), distances.end()); std::sort(ovens.begin(), ovens.end(), cmpByCost); auto startingDistance = distances.begin(); ULL mul = 0; long long acc = 0; long long ZERO = 0; for(auto oven = ovens.begin(); oven != ovens.end(); ++oven) { // if(oven != ovens.begin()) { // auto previous = oven - 1; // oven->minimalWaitingTime = previous->minimalWaitingTime + ((mul) * (mul + 1) / 2) * (oven->cost - previous->cost) /*(mul * (oven->cost - previous->cost))*/; // if (acc < 0) { // oven->minimalWaitingTime += acc; // } // } acc = 0; for(auto startingDistance = distances.begin();startingDistance != distances.end() /*&& (*startingDistance < oven->cost)*/; ++startingDistance) { if(acc < 0) { // acc = 0; } long long diff = oven->cost - *startingDistance; if(diff > 0) { acc += diff; } else { acc = 0; } oven->minimalWaitingTime += std::max(acc, ZERO); mul++; } } std::sort(ovens.begin(), ovens.end(), cmpByIdx); for(auto oven = ovens.begin(); oven != ovens.end(); ++oven) { std::cout<<oven->minimalWaitingTime<<std::endl; } 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 | #include <iostream> #include <vector> #include <string> #include <algorithm> #include <deque> #include <stack> #include <iterator> #include <numeric> #include <tuple> #include <set> #include <functional> #include <map> typedef unsigned long long ULL; typedef long long LL; struct Oven { LL cost; int idx; ULL minimalWaitingTime; }; Oven createOven(int idx, LL cost) { Oven o; o.idx = idx; o.cost = cost; o.minimalWaitingTime = 0; return o; } bool cmpByCost(const Oven& lhs, const Oven& rhs) { return lhs.cost < rhs.cost; } bool cmpByIdx(const Oven& lhs, const Oven& rhs) { return lhs.idx < rhs.idx; } int main(int argc, char **argv) { int numberOfCustomers, numberOfOvens; std::cin>>numberOfCustomers>>numberOfOvens; std::vector<ULL> times; std::vector<Oven> ovens; times.reserve(numberOfCustomers); ovens.reserve(numberOfOvens); for(int i = 0; i < numberOfCustomers; ++i) { ULL t; std::cin>>t; times.push_back(t); } for(int i = 0; i < numberOfOvens; ++i) { LL t; std::cin>>t; ovens.push_back(createOven(i, t)); } std::vector<ULL> distances; distances.push_back(times[0]); for(auto i = 1UL; i < times.size(); ++i) { distances.push_back(times[i] - times[i - 1]); } // std::sort(distances.begin(), distances.end()); std::sort(ovens.begin(), ovens.end(), cmpByCost); auto startingDistance = distances.begin(); ULL mul = 0; long long acc = 0; long long ZERO = 0; for(auto oven = ovens.begin(); oven != ovens.end(); ++oven) { // if(oven != ovens.begin()) { // auto previous = oven - 1; // oven->minimalWaitingTime = previous->minimalWaitingTime + ((mul) * (mul + 1) / 2) * (oven->cost - previous->cost) /*(mul * (oven->cost - previous->cost))*/; // if (acc < 0) { // oven->minimalWaitingTime += acc; // } // } acc = 0; for(auto startingDistance = distances.begin();startingDistance != distances.end() /*&& (*startingDistance < oven->cost)*/; ++startingDistance) { if(acc < 0) { // acc = 0; } long long diff = oven->cost - *startingDistance; if(diff > 0) { acc += diff; } else { acc = 0; } oven->minimalWaitingTime += std::max(acc, ZERO); mul++; } } std::sort(ovens.begin(), ovens.end(), cmpByIdx); for(auto oven = ovens.begin(); oven != ovens.end(); ++oven) { std::cout<<oven->minimalWaitingTime<<std::endl; } return 0; } |