Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <map>
  4. #include <set>
  5. #include <string>
  6. #include <vector>
  7. #include <sstream>
  8. #include <cmath>
  9. #include <cstring>
  10. #include <cstdio>
  11. #include <ctime>
  12. #include <cassert>
  13. using namespace std;
  14.  
  15. const int N = 505;
  16. int x, a, y, b, len;
  17. pair<int, int> dp[N][N];
  18.  
  19. bool can(int w)
  20. {
  21.     for (int i = 0; i <= x; i++)
  22.         for (int j = 0; j <= y; j++)
  23.             dp[i][j] = make_pair(0, 0);
  24.  
  25.     for (int i = 0; i <= x; i++)
  26.         for (int j = 0; j <= y; j++)
  27.         {
  28.             int rows = dp[i][j].first;
  29.             int rem = dp[i][j].second;
  30.            
  31.             int rem1 = rem + a;
  32.             int rows1 = rows + (rem1 >= w);
  33.             if (rem1 >= w)
  34.                 rem1 = 0;
  35.             dp[i + 1][j] = max(dp[i + 1][j], make_pair(rows1, rem1));
  36.  
  37.             int rem2 = rem + b;
  38.             int rows2 = rows + (rem2 >= w);
  39.             if (rem2 >= w)
  40.                 rem2 = 0;
  41.             dp[i][j + 1] = max(dp[i][j + 1], make_pair(rows2, rem2));
  42.         }
  43.  
  44.  
  45.     for (int i = 0; i <= x; i++)
  46.         for (int j = 0; j <= y; j++)
  47.             if (dp[i][j].first >= len)
  48.                 return true;
  49.     return false;
  50. }
  51.  
  52. bool solve()
  53. {
  54.     if (scanf("%d%d%d%d%d", &x, &a, &y, &b, &len) != 5)
  55.         return false;
  56.  
  57.     int l = 0;
  58.     int r = (x * a + y * b) / len + 1;
  59.     while (r - l > 1)
  60.     {
  61.         int m = (l + r) / 2;
  62.         if (can(m))
  63.             l = m;
  64.         else
  65.             r = m;
  66.     }
  67.  
  68.     printf("%d\n", l);
  69.  
  70.     return true;
  71. }
  72.  
  73. int main()
  74. {
  75. #ifdef LOCAL
  76.     freopen("input.txt", "r", stdin);
  77. #endif
  78.  
  79.     while (solve()) {}
  80.  
  81.  
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement