Advertisement
theo830

κουπόνια

Apr 27th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. int T[100][1000];
  8. int q[100];
  9. int o = 0;
  10. void printknapSack(int W, int wt[], int n) {
  11. int i, w;
  12. int T[n + 1][W + 1];
  13. for (i = 0; i <= n; i++) {
  14. for (w = 0; w <= W; w++) {
  15. if (i == 0 || w == 0)
  16. T[i][w] = 0;
  17. else if (wt[i - 1] <= w)
  18. T[i][w] = max(wt[i - 1] + T[i - 1][w - wt[i - 1]], T[i - 1][w]);
  19. else
  20. T[i][w] = T[i - 1][w];
  21. }
  22. }
  23. int res = T[n][W];
  24. w = W;
  25. for (i = n; i > 0 && res > 0; i--) {
  26. if (res == T[i - 1][w])
  27. continue;
  28. else {
  29. q[o] = wt[i-1];
  30. o = o + 1;
  31. res = res - wt[i - 1];
  32. w = w - wt[i - 1];
  33. }
  34. }
  35. }
  36. int main() {
  37. int n;
  38. cin>>n;
  39. int wt[n];
  40. int w1[n];
  41. for(int i=0;i<n;i++){
  42. cin>>w1[i];
  43. if(w1[i] % 10 != 0){
  44. wt[i] = (w1[i]/10)+1;
  45. }
  46. else{
  47. wt[i]= w1[i]/10;
  48. }
  49. }
  50. int k;
  51. cin>>k;
  52. k++;
  53. int w;
  54. w = k-1;
  55. int T[n][k];
  56. for(int i=0;i<k;i++){
  57. T[0][i] = 0;
  58. }
  59. for(int i=0;i<n;i++){
  60. for(int j=0;j<k;j++){
  61. if(j<wt[i]){
  62. T[i][j] = T[i-1][j];
  63. }
  64. else{
  65. T[i][j] = max(wt[i]+T[i-1][j-wt[i]],T[i-1][j]);
  66. }
  67. }
  68. }
  69. printknapSack(w,wt,n);
  70. sort(q,q+o);
  71. for(int i=0;i<o;i++){
  72. cout<<q[i]<<endl;
  73. }
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement