#include <stdio.h> #include <vector> #include <algorithm> using std::vector; using std::lower_bound; using std::sort; struct field { int speed; long growth; bool operator<(const struct field &one) const { return speed < one.speed; } bool operator<(const int &one) const { return speed < one; } void operator=(const struct field &one) { speed = one.speed; growth = one.growth; } }; int main() { int n, m; scanf("%d %d", &n, &m); struct field *field = new struct field[n]; for (int i = 0; i < n; i++) { scanf("%d", &field[i].speed); } //sort(field, field + n); long last_cut = 0; long last_depth = 0; for (int i = 0; i < m; i++) { long duration; long depth; long tmp; scanf("%d %ld", &duration, &depth); /* determine how long the grass has been growing for */ tmp = duration; duration -= last_cut; last_cut = tmp; /* determine the minmal speed that will be affected */ /* assuming all fields were cut to last_depth depth on last_cut */ // int min = (depth - last_depth) / duration; // struct field *min_field; // min_field = lower_bound(field, field + n, min); // int j = (field - min_field) / sizeof(struct field); /* scan through and update the irrelevant fields */ /* change V*/ int j; for (j = 0; j < n; j++) { if (field[j].speed * duration + field[j].growth <= depth) { field[j].growth += field[j].speed * duration; } else break; } long long total = 0; for (; j < n; j++) { total += field[j].growth + duration * field[j].speed - depth; field[j].growth = depth; } printf("%lld\n", total); last_depth = depth; } 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 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 86 87 88 | #include <stdio.h> #include <vector> #include <algorithm> using std::vector; using std::lower_bound; using std::sort; struct field { int speed; long growth; bool operator<(const struct field &one) const { return speed < one.speed; } bool operator<(const int &one) const { return speed < one; } void operator=(const struct field &one) { speed = one.speed; growth = one.growth; } }; int main() { int n, m; scanf("%d %d", &n, &m); struct field *field = new struct field[n]; for (int i = 0; i < n; i++) { scanf("%d", &field[i].speed); } //sort(field, field + n); long last_cut = 0; long last_depth = 0; for (int i = 0; i < m; i++) { long duration; long depth; long tmp; scanf("%d %ld", &duration, &depth); /* determine how long the grass has been growing for */ tmp = duration; duration -= last_cut; last_cut = tmp; /* determine the minmal speed that will be affected */ /* assuming all fields were cut to last_depth depth on last_cut */ // int min = (depth - last_depth) / duration; // struct field *min_field; // min_field = lower_bound(field, field + n, min); // int j = (field - min_field) / sizeof(struct field); /* scan through and update the irrelevant fields */ /* change V*/ int j; for (j = 0; j < n; j++) { if (field[j].speed * duration + field[j].growth <= depth) { field[j].growth += field[j].speed * duration; } else break; } long long total = 0; for (; j < n; j++) { total += field[j].growth + duration * field[j].speed - depth; field[j].growth = depth; } printf("%lld\n", total); last_depth = depth; } return 0; } |