Advertisement
Nita_Cristian

rucsac dinamica

Feb 4th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("rucsac.in");
  6. ofstream fout("rucsac.out");
  7.  
  8. int N, G, k = 1;
  9. int p[10001], g[10001], dp[2][10001];
  10.  
  11. int main()
  12. {
  13.     fin >> N >> G; // citesc numarul de obiecte si greutatea maxima
  14.     for(int i = 1; i <= N; i++)
  15.         fin >> g[i] >> p[i]; // citesc greutatea si profitul obiectului
  16.  
  17.     for(int i = 1; i <= N; i++) // parcurg fiecare obiect
  18.     {
  19.         for(int j = 1; j <= G; j++) //
  20.             if(j - g[i] >= 0)// daca obiectul incape in ghiozdan
  21.                 dp[k][j] = max(dp[1-k][j], dp[1-k][j - g[i]] + p[i]), fout << dp[k][j] << ' ';// profitul maxim va fi cel de deasupra sau profitul obiectului plus profitul celorlalte obiecte
  22.  
  23.             else
  24.                 dp[k][j] = dp[1-k][j]; // altfel profitul maxim este cel cu o linie mai sus
  25.  
  26.         k = 1 - k, fout << '\n';
  27.     }
  28.  
  29.     fout << dp[1-k][G];
  30.  
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement