-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathP4095.c
81 lines (71 loc) · 1.87 KB
/
P4095.c
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
#include <stdio.h>
#include <stdlib.h>
#define maxn 1000
#define maxe 1000
int dp[2][maxn][maxe + 1];
typedef struct {
int index;
int value;
}Node;
Node queue[maxe];
int head, tail;
void Init() {
head = 0;
tail = -1;
}
void Push(int index, int value) {
while (tail >= head && queue[tail].value < value) {
tail--;
}
tail++;
queue[tail].index = index;
queue[tail].value = value;
}
void Check(int index) {
if (queue[head].index < index) head++;
}
int Top() {
return queue[head].value;
}
int max(int a, int b) {
return a > b ? a : b;
}
int val[maxn], cost[maxn], num[maxn];
int main() {
int n, q, d, e, ans;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &cost[i], &val[i], &num[i]);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < cost[i]; j++) {
Init();
for (int k = 0; k <= (maxe - j) / cost[i]; k++) {
Push(k, (i ? dp[0][i - 1][k * cost[i] + j] : 0) - k * val[i]);
Check(k - num[i]);
dp[0][i][k * cost[i] + j] = Top() + k * val[i];
}
}
for (int i = n - 1; i >= 0; i--)
for (int j = 0; j < cost[i]; j++) {
Init();
for (int k = 0; k <= (maxe - j) / cost[i]; k++) {
Push(k, (i < n - 1 ? dp[1][i + 1][k * cost[i] + j] : 0) - k * val[i]);
Check(k - num[i]);
dp[1][i][k * cost[i] + j] = Top() + k * val[i];
}
}
scanf("%d", &q);
for (int i = 0; i < q; i++) {
scanf("%d %d", &d, &e);
ans = 0;
if (d > 0 && d < n - 1) {
for (int j = 0; j <= e; j++)
ans = max(ans, dp[0][d - 1][j] + dp[1][d + 1][e - j]);
}
else if (d == 0) ans = dp[1][1][e];
else ans = dp[0][n - 2][e];
printf("%d\n", ans);
}
exit(0);
}