#include <algorithm>
#include <iostream>
#include <vector>
using Number = unsigned long long;
Number calc(int cookTime, const std::vector<Number> &clients)
{
Number sum{};
Number lowestStartTime{};
for (const auto &clientTime : clients)
{
Number clientStartTime = clientTime - cookTime;
if (clientTime < cookTime)
{
clientStartTime = 0;
}
if (lowestStartTime <= clientStartTime)
{
if (clientTime < lowestStartTime + cookTime)
{
sum += lowestStartTime + cookTime - clientTime;
}
lowestStartTime = clientTime;
}
else
{
sum += lowestStartTime - clientStartTime;
lowestStartTime += cookTime;
}
}
return sum;
}
int main()
{
int n_clients;
int n_ovens;
std::cin >> n_clients >> n_ovens;
std::vector<Number> clients(n_clients);
for (auto& client : clients)
{
std::cin >> client;
}
std::vector<int> ovens(n_ovens);
for (auto& oven : ovens)
{
std::cin >> oven;
}
for (const auto &oven: ovens)
{
std::cout << calc(oven, clients) <<"\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 | #include <algorithm> #include <iostream> #include <vector> using Number = unsigned long long; Number calc(int cookTime, const std::vector<Number> &clients) { Number sum{}; Number lowestStartTime{}; for (const auto &clientTime : clients) { Number clientStartTime = clientTime - cookTime; if (clientTime < cookTime) { clientStartTime = 0; } if (lowestStartTime <= clientStartTime) { if (clientTime < lowestStartTime + cookTime) { sum += lowestStartTime + cookTime - clientTime; } lowestStartTime = clientTime; } else { sum += lowestStartTime - clientStartTime; lowestStartTime += cookTime; } } return sum; } int main() { int n_clients; int n_ovens; std::cin >> n_clients >> n_ovens; std::vector<Number> clients(n_clients); for (auto& client : clients) { std::cin >> client; } std::vector<int> ovens(n_ovens); for (auto& oven : ovens) { std::cin >> oven; } for (const auto &oven: ovens) { std::cout << calc(oven, clients) <<"\n"; } return 0; } |
English