#include <algorithm> #include <cstdio> #include <map> #define ZAP_ENABLED using namespace std; static const int N = 200000; static long long t[N]; static int m[N]; static map<int, long long> cache; static long long waitingTime(const long long d, const int n) { if (cache.find(d) == cache.end()) { long long wt = m[0]*(2*max(d - t[0], 0LL) + (m[0] - 1)*d)/2; long long w = wt; for(int i = 1; i < n; i++) { const long long t1 = max(t[i - 1] + wt, t[i] - d); wt = m[i]*(2*(t1 + d - t[i]) + (m[i] - 1)*d)/2; w += wt; } cache[d] = w; } return cache[d]; } #ifdef ZAP_ENABLED int main() { int n0, k, n = 1, d; scanf("%d%d%lld", &n0, &k, t); m[0] = 1; for(int i = 1; i < n0; i++) { scanf("%lld", t + n); const bool r = (t[n] == t[n - 1]); m[n - 1] += r; m[n] = 1; n += !r; } for(int i = 0; i < k; i++) { scanf("%d", &d); printf("%lld\n", waitingTime(d, n)); } return 0; } #endif
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 | #include <algorithm> #include <cstdio> #include <map> #define ZAP_ENABLED using namespace std; static const int N = 200000; static long long t[N]; static int m[N]; static map<int, long long> cache; static long long waitingTime(const long long d, const int n) { if (cache.find(d) == cache.end()) { long long wt = m[0]*(2*max(d - t[0], 0LL) + (m[0] - 1)*d)/2; long long w = wt; for(int i = 1; i < n; i++) { const long long t1 = max(t[i - 1] + wt, t[i] - d); wt = m[i]*(2*(t1 + d - t[i]) + (m[i] - 1)*d)/2; w += wt; } cache[d] = w; } return cache[d]; } #ifdef ZAP_ENABLED int main() { int n0, k, n = 1, d; scanf("%d%d%lld", &n0, &k, t); m[0] = 1; for(int i = 1; i < n0; i++) { scanf("%lld", t + n); const bool r = (t[n] == t[n - 1]); m[n - 1] += r; m[n] = 1; n += !r; } for(int i = 0; i < k; i++) { scanf("%d", &d); printf("%lld\n", waitingTime(d, n)); } return 0; } #endif |