#include <iostream> using namespace std; long long T[200001]; long long solutions[1000001]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", &T[i]); } for (int j = 0; j <= 1000000; j++) { solutions[j] = -1; } long long threshold = 1000001LL; for (int j = 0; j < m; j++) { long long answer; long long d; scanf("%lld", &d); if (d >= threshold) { answer = solutions[threshold] + (1LL * n * (n + 1) / 2) * (d - threshold); } else if (solutions[d] != -1) { answer = solutions[d]; } else { answer = 0; long long last = 0; int customers_waiting = 0; for (int i = 1; i <= n; i++) { long long diff = T[i] - last; if (diff >= d) { last = T[i]; } else { last += d; answer += last - T[i]; customers_waiting++; } } solutions[d] = answer; if (customers_waiting == n) { threshold = d; } } printf("%lld\n", answer); } 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 | #include <iostream> using namespace std; long long T[200001]; long long solutions[1000001]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", &T[i]); } for (int j = 0; j <= 1000000; j++) { solutions[j] = -1; } long long threshold = 1000001LL; for (int j = 0; j < m; j++) { long long answer; long long d; scanf("%lld", &d); if (d >= threshold) { answer = solutions[threshold] + (1LL * n * (n + 1) / 2) * (d - threshold); } else if (solutions[d] != -1) { answer = solutions[d]; } else { answer = 0; long long last = 0; int customers_waiting = 0; for (int i = 1; i <= n; i++) { long long diff = T[i] - last; if (diff >= d) { last = T[i]; } else { last += d; answer += last - T[i]; customers_waiting++; } } solutions[d] = answer; if (customers_waiting == n) { threshold = d; } } printf("%lld\n", answer); } return 0; } |