Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.   int x,a,y,b,l;
  7.   pair<int,int> dp[505][505];
  8.   while (cin >> x >> a >> y >> b >> l) {
  9.     int ll = min(a,b), rr = (a*x + b*y)/l;
  10.     while (ll < rr) {
  11.       int mm = (ll+rr+1)/2;
  12.       int onea = (mm+a-1)/a;
  13.       int oneb = (mm+b-1)/b;
  14.       for (int i = 0; i <= y; i++) {
  15.         dp[0][i] = make_pair(i/oneb, i%oneb);
  16.       }
  17.       for (int i = 1; i <= x; i++) {
  18.         dp[i][0] = make_pair(i/onea, i%onea);
  19.         for (int j = 1; j <= y; j++) {
  20.           dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
  21.           if (dp[i][j-1].second+b < mm) {
  22.             dp[i][j] = max(dp[i][j], make_pair(dp[i][j-1].first, dp[i][j-1].second+b));
  23.           } else {
  24.             dp[i][j] = max(dp[i][j], make_pair(dp[i][j-1].first+1, 0));
  25.           }
  26.           if (dp[i-1][j].second+a < mm) {
  27.             dp[i][j] = max(dp[i][j], make_pair(dp[i-1][j].first, dp[i-1][j].second+a));
  28.           } else {
  29.             dp[i][j] = max(dp[i][j], make_pair(dp[i-1][j].first+1, 0));
  30.           }
  31.         }
  32.       }
  33.       if (dp[x][y].first >= l) {
  34.         ll = mm;
  35.       } else {
  36.         rr = mm-1;
  37.       }
  38.     }
  39.     cout << ll << endl;
  40.   }
  41.   return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement