sadiqul_amin

uva 990 wa

Mar 29th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define MAX(x, y) (x>y)?x:y
  4.  
  5. int t, w;
  6. int n;
  7. int d[1010], v[1010];
  8. int check[1010];
  9. int cnt = 0;
  10. int track[1010][1010];
  11.  
  12. void inputCase()
  13. {
  14.     int i;
  15.     scanf("%d", &t);
  16.     scanf("%d", &w);
  17.     scanf("%d", &n);
  18.  
  19.     for (i = 0; i < n; i++)
  20.         scanf("%d%d", &d[i], &v[i]);
  21. }
  22.  
  23. int solve(int time, int gold, int item)
  24. {
  25.     int x, y;
  26.  
  27.     if (item >= n)
  28.     {
  29.         track[item][time] = -1;
  30.         return gold;
  31.     }
  32.  
  33.     if (time == 0)
  34.     {
  35.         track[item][time] = -1;
  36.         return gold;
  37.     }
  38.  
  39.     if (time < 0)
  40.     {
  41.         track[item][time] = -1;
  42.         return gold - v[item - 1];
  43.     }
  44.  
  45.     x = solve(time, gold, item + 1);
  46.     y = solve(time - (3 * w * d[item]), gold + v[item], item + 1);
  47.  
  48.     if (x > y)
  49.     {
  50.         track[item][time] = 2;
  51.         return x;
  52.     }
  53.     else
  54.     {
  55.         track[item][time] = 1;
  56.         return y;
  57.     }
  58. }
  59.  
  60.  
  61. void printNum(int time, int item)
  62. {
  63.     if (item >= n)
  64.         return ;
  65.  
  66.     if (time == 0)
  67.         return ;
  68.  
  69.     if (time < 0)
  70.         return;
  71.  
  72.     if (track[item][time] == 2)
  73.         printNum(time, item + 1);
  74.     else if (track[item][time] == 1)
  75.     {
  76.         cnt++;
  77.         check[item] = 1;
  78.         printNum(time - (3 * w * d[item]), item + 1);
  79.     }
  80.     else if (track[item][time] == -1)
  81.     {
  82.         return;
  83.     }
  84. }
  85.  
  86.  
  87. int main()
  88. {
  89.     int i;
  90.  
  91.     inputCase();
  92.     printf("%d\n", solve(t, 0, 0));
  93.  
  94. /*  for (i = 0; i < 10; i++)
  95.     {
  96.         if (check[i] == 1)
  97.             cnt++;
  98.     }
  99. */
  100.     printNum(t, 0);
  101.     printf("%d\n", cnt);
  102.     for (i = 0; i < 40; i++)
  103.     {
  104.         if (check[i] == 1)
  105.             printf("%d %d\n", d[i], v[i]);
  106.     }
  107.     return 0;
  108. }
Add Comment
Please, Sign In to add comment