#include <stdio.h> #include <stdlib.h> static int cmp(const void *a, const void *b) { long long int x = *(long long int *)a; long long int y = *(long long int *)b; if (x < y) { return -1; } else if (x == y) { return 0; } else { return 1; } } /* * Locate the leftmost value that is greater than or eqaul to the key. * Return N if no such value exists. */ static int binary_search(long long int *array, int n, long long int multiplier, long long int key) { int left, right, middle; left = 0; right = n; while (left < right) { middle = (left + right) / 2; if (multiplier * array[middle] < key) { left = middle + 1; } else { right = middle; } } return left; } static void compute_suffix_sums(long long int *source, int n, long long int *dest) { long long int sum = 0LL; int i; for (i = 0; i < n; i++) { sum = sum + source[n - i - 1]; dest[n - i - 1] = sum; } } int main() { long long int *delta; long long int *suffix_sums; int n, i, m, first_cut; long long int day, height, value, sum; scanf("%d %d", &n, &m); delta = calloc(n, sizeof(long long int)); suffix_sums = calloc(n, sizeof(long long int)); for (i = 0; i < n; i++) { scanf("%lld", delta + i); } qsort(delta, n, sizeof(long long int), cmp); compute_suffix_sums(delta, n, suffix_sums); sum = 0LL; for (i = 0; i < n; i++) { scanf("%lld %lld", &day, &height); first_cut = binary_search(delta, n, day, height); if (first_cut < n) { value = day * suffix_sums[first_cut] - (n - first_cut) * height; } else { value = 0; } value -= sum; value = value > 0 ? value : 0; printf("%lld\n", value); sum += value; } return EXIT_SUCCESS; }
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include <stdio.h> #include <stdlib.h> static int cmp(const void *a, const void *b) { long long int x = *(long long int *)a; long long int y = *(long long int *)b; if (x < y) { return -1; } else if (x == y) { return 0; } else { return 1; } } /* * Locate the leftmost value that is greater than or eqaul to the key. * Return N if no such value exists. */ static int binary_search(long long int *array, int n, long long int multiplier, long long int key) { int left, right, middle; left = 0; right = n; while (left < right) { middle = (left + right) / 2; if (multiplier * array[middle] < key) { left = middle + 1; } else { right = middle; } } return left; } static void compute_suffix_sums(long long int *source, int n, long long int *dest) { long long int sum = 0LL; int i; for (i = 0; i < n; i++) { sum = sum + source[n - i - 1]; dest[n - i - 1] = sum; } } int main() { long long int *delta; long long int *suffix_sums; int n, i, m, first_cut; long long int day, height, value, sum; scanf("%d %d", &n, &m); delta = calloc(n, sizeof(long long int)); suffix_sums = calloc(n, sizeof(long long int)); for (i = 0; i < n; i++) { scanf("%lld", delta + i); } qsort(delta, n, sizeof(long long int), cmp); compute_suffix_sums(delta, n, suffix_sums); sum = 0LL; for (i = 0; i < n; i++) { scanf("%lld %lld", &day, &height); first_cut = binary_search(delta, n, day, height); if (first_cut < n) { value = day * suffix_sums[first_cut] - (n - first_cut) * height; } else { value = 0; } value -= sum; value = value > 0 ? value : 0; printf("%lld\n", value); sum += value; } return EXIT_SUCCESS; } |