【HDOJ】3466 Proud Merchants
先排序预处理,后01背包。
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define MAX(a, b) (a>b) ? a:b 6 7 int dp[5000]; 8 9 typedef struct { 10 int p, q, v; 11 } stuff_st; 12 13 stuff_st stuffs[505]; 14 15 int comp(const void *a, const void *b) { 16 stuff_st *p = (stuff_st *)a; 17 stuff_st *q = (stuff_st *)b; 18 19 return (p->q-p->p) - (q->q-q->p); 20 } 21 22 int main() { 23 int n, m; 24 int i, j; 25 26 while (scanf("%d %d", &n, &m) != EOF) { 27 for (i=1; i<=n; ++i) { 28 scanf("%d%d%d", &stuffs[i].p, &stuffs[i].q, &stuffs[i].v); 29 } 30 memset(dp, 0, sizeof(dp)); 31 qsort(stuffs+1, n, sizeof(stuff_st), comp); 32 for (i=1; i<=n; ++i) { 33 for (j=m; j>=stuffs[i].q; --j) { 34 dp[j] = MAX(dp[j], dp[j-stuffs[i].p]+stuffs[i].v); 35 } 36 /* 37 for (j=0; j<=m; ++j) { 38 printf("%d ", dp[j]); 39 } 40 printf("\n"); 41 */ 42 } 43 printf("%d\n", dp[m]); 44 } 45 46 return 0; 47 }