#include <iostream>
#include <map>
long long wait_time(const std::map<long long, int> &T, long long D)
{
long long wait_time = 0;
long long current_time = 0;
for (const auto &i : T)
{
if (current_time + D <= i.first)
{
current_time = i.first;
}
else
{
wait_time += std::min(current_time + D - i.first, D);
current_time += D;
}
current_time += (i.second - 1) * D;
wait_time += (i.second - 1) * D;
}
return wait_time;
}
int main()
{
std::ios_base::sync_with_stdio(0);
//
int N = 0;
int M = 0;
std::cin >> N;
std::cin >> M;
std::map<long long, int> T;
for (int i = 0; i < N; ++i)
{
long long temp = 0; std::cin >> temp;
T[temp]++;
}
//
for (int i = 0; i < M; ++i)
{
long long D = 0; std::cin >> D;
printf("%lld\n", wait_time(T, D));
}
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 | #include <iostream> #include <map> long long wait_time(const std::map<long long, int> &T, long long D) { long long wait_time = 0; long long current_time = 0; for (const auto &i : T) { if (current_time + D <= i.first) { current_time = i.first; } else { wait_time += std::min(current_time + D - i.first, D); current_time += D; } current_time += (i.second - 1) * D; wait_time += (i.second - 1) * D; } return wait_time; } int main() { std::ios_base::sync_with_stdio(0); // int N = 0; int M = 0; std::cin >> N; std::cin >> M; std::map<long long, int> T; for (int i = 0; i < N; ++i) { long long temp = 0; std::cin >> temp; T[temp]++; } // for (int i = 0; i < M; ++i) { long long D = 0; std::cin >> D; printf("%lld\n", wait_time(T, D)); } return 0; } |
English