Alex_tz307

Fundamente XI - Etalon - 209

Sep 20th, 2020 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. inline void drum(int k, vector < int >& t) {
  6.     if(k != 0) {
  7.         cout << t[k] << ' ';
  8.         drum(k - t[k], t);
  9.     }
  10. }
  11.  
  12. int main() {
  13.     ios_base::sync_with_stdio(false);
  14.     cin.tie(nullptr);
  15.     cout.tie(nullptr);
  16.     int n, k;
  17.     cin >> n >> k;
  18.     vector < int > a(n + 1);
  19.     for(int i = 1; i <= n; ++i)
  20.         cin >> a[i];
  21.     vector < bool > dp(k + 1); // dp[i] - pot cantari greutatea i?
  22.     vector < int > t(k + 1); // t[x] - greutatea ultimului etalon folosit pentru a masura greutatea x
  23.     // pentru a evita folosirea unui etalon de mai multe ori parcurg dp invers
  24.     dp[0] = true;
  25.     int mx = 0;
  26.     for(int i = 1; i <= n; ++i)
  27.         for(int j = mx; j >= 0; --j)
  28.             if(dp[j] &&  j + a[i] <= k && !dp[j + a[i]]) {
  29.                 dp[j + a[i]] = true;
  30.                 t[j + a[i]] = a[i];
  31.                 mx = max(mx, j + a[i]);
  32.             }
  33.     if(!dp[k])
  34.         cout << "Imposibil";
  35.     else
  36.         drum(k, t);
  37. }
  38.  
Add Comment
Please, Sign In to add comment