Dang_Quan_10_Tin

HINHTHANG 27.12.2021

Dec 27th, 2021 (edited)
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #define task "HINHTHANG"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. constexpr int N = 8e1 + 5;
  12. constexpr int Inf = 1e9 + 7;
  13. int n;
  14. ll S, a[N], b[N];
  15. int dp[N][N];
  16.  
  17. void Read()
  18. {
  19.     cin >> n >> S;
  20.     ++n;
  21.     for (int i = 1; i <= n; ++i)
  22.     {
  23.         cin >> a[i];
  24.         a[i] += a[i - 1];
  25.     }
  26.     for (int i = 1; i <= n; ++i)
  27.     {
  28.         cin >> b[i];
  29.         b[i] += b[i - 1];
  30.     }
  31. }
  32.  
  33. void Solve()
  34. {
  35.     fill_n(&dp[0][0], N * N, Inf);
  36.  
  37.     for (int i = 2; i <= n; ++i)
  38.         for (int j = 2; j <= n; ++j)
  39.         {
  40.             if (a[i] + b[j] <= S)
  41.                 dp[i][j] = 1;
  42.             else
  43.                 for (int t = i - 1; t; --t)
  44.                     for (int h = j - 1; h; --h)
  45.                         if ((a[i] - a[t - 1]) + (b[j] - b[h - 1]) <= S) // cost <= S
  46.                             dp[i][j] = min(dp[i][j], dp[t][h] + 1);
  47.         }
  48.     cout << (dp[n][n] >= Inf ? -1 : dp[n][n]);
  49. }
  50.  
  51. int32_t main()
  52. {
  53.     ios::sync_with_stdio(0);
  54.     cin.tie(0);
  55.     cout.tie(0);
  56.     if (fopen(task ".INP", "r"))
  57.     {
  58.         freopen(task ".INP", "r", stdin);
  59.         freopen(task ".OUT", "w", stdout);
  60.     }
  61.     Read();
  62.     Solve();
  63. }
  64.  
Add Comment
Please, Sign In to add comment