zju 3211
晕死,一道简单的DP,竟然花了6个小时,没天理啊,真是傻到家了。
现在把代码贴出来,以后可以参考一下。
View Code
1 //
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6 typedef struct node
7 {
8 int x, y;
9 }Node;
10
11 Node nodes[310];
12
13 int Compare(const void *p1, const void *p2);
14 int main(int argc, char *argv[])
15 {
16 int t;
17
18 scanf("%d", &t);
19
20 while(t-- > 0)
21 {
22 int a, b;
23
24
25 int result[310][310] = {0};
26
27 scanf("%d %d", &a, &b);
28
29 memset(nodes, 0, sizeof(Node)*310);
30
31 for(int i = 0; i < a; i++)
32 scanf("%d", &nodes[i].x);
33
34 for(int i = 0; i < a; i++)
35 scanf("%d", &nodes[i].y);
36
37 qsort(nodes, a, sizeof(Node), Compare);
38
39 for(int i = 0; i < b; i++)
40 result[0][i] = nodes[0].x + i*nodes[0].y;
41
42 for(int i = 1; i < a; i++)
43 {
44 result[i][0] = nodes[i].x > result[i-1][0] ? nodes[i].x : result[i-1][0];
45 }
46
47 for(int i = 1; i < a; i++)
48 {
49 for(int j = 1; j < b; j++)
50 {
51 int x = result[i-1][j];
52 int y = result[i-1][j-1] + nodes[i].x + j*nodes[i].y;
53
54 result[i][j] = x > y ? x : y;
55 }
56 }
57
58 printf("%d\n", result[a-1][b-1]);
59 }
60 }
61
62
63 int Compare(const void *p1, const void *p2)
64 {
65 Node* n1 = (Node*)p1;
66 Node* n2 = (Node*)p2;
67
68 return n1->y - n2->y;
69 }
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6 typedef struct node
7 {
8 int x, y;
9 }Node;
10
11 Node nodes[310];
12
13 int Compare(const void *p1, const void *p2);
14 int main(int argc, char *argv[])
15 {
16 int t;
17
18 scanf("%d", &t);
19
20 while(t-- > 0)
21 {
22 int a, b;
23
24
25 int result[310][310] = {0};
26
27 scanf("%d %d", &a, &b);
28
29 memset(nodes, 0, sizeof(Node)*310);
30
31 for(int i = 0; i < a; i++)
32 scanf("%d", &nodes[i].x);
33
34 for(int i = 0; i < a; i++)
35 scanf("%d", &nodes[i].y);
36
37 qsort(nodes, a, sizeof(Node), Compare);
38
39 for(int i = 0; i < b; i++)
40 result[0][i] = nodes[0].x + i*nodes[0].y;
41
42 for(int i = 1; i < a; i++)
43 {
44 result[i][0] = nodes[i].x > result[i-1][0] ? nodes[i].x : result[i-1][0];
45 }
46
47 for(int i = 1; i < a; i++)
48 {
49 for(int j = 1; j < b; j++)
50 {
51 int x = result[i-1][j];
52 int y = result[i-1][j-1] + nodes[i].x + j*nodes[i].y;
53
54 result[i][j] = x > y ? x : y;
55 }
56 }
57
58 printf("%d\n", result[a-1][b-1]);
59 }
60 }
61
62
63 int Compare(const void *p1, const void *p2)
64 {
65 Node* n1 = (Node*)p1;
66 Node* n2 = (Node*)p2;
67
68 return n1->y - n2->y;
69 }