#include <cstdio> using namespace std; int solve(int* clients, int n, int t) { int waitTime = 0; int busyUntil = t; if(clients[0] < t) { waitTime = t - clients[0]; busyUntil = t; } else { waitTime = 0; busyUntil = clients[0]; } for(int i = 1; i < n; i++) { if(busyUntil + t <= clients[i]) { busyUntil = clients[i]; waitTime += 0; } else { // busyUntil + t > clients[i] busyUntil = busyUntil + t; waitTime += busyUntil - clients[i]; } } return waitTime; } int main() { int n,m; scanf("%d %d", &n, &m); int clients[n]; for(int i = 0; i < n; ++i) { scanf("%d", &clients[i]); } for(int i = 0; i < m; ++i) { int t; scanf("%d", &t); printf("%d\n", solve(clients, n, t)); } 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 | #include <cstdio> using namespace std; int solve(int* clients, int n, int t) { int waitTime = 0; int busyUntil = t; if(clients[0] < t) { waitTime = t - clients[0]; busyUntil = t; } else { waitTime = 0; busyUntil = clients[0]; } for(int i = 1; i < n; i++) { if(busyUntil + t <= clients[i]) { busyUntil = clients[i]; waitTime += 0; } else { // busyUntil + t > clients[i] busyUntil = busyUntil + t; waitTime += busyUntil - clients[i]; } } return waitTime; } int main() { int n,m; scanf("%d %d", &n, &m); int clients[n]; for(int i = 0; i < n; ++i) { scanf("%d", &clients[i]); } for(int i = 0; i < m; ++i) { int t; scanf("%d", &t); printf("%d\n", solve(clients, n, t)); } return 0; } |