Advertisement
nna42799

Untitled

Dec 17th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using std::cin, std::cout, std::endl;
  7. using std::fstream;
  8. using std::max;
  9. using std::ostream;
  10. using std::sort;
  11. using std::vector;
  12.  
  13. using LL = long long;
  14. const char endq = '\n';
  15.  
  16. class ItemInfo
  17. {
  18. public:
  19.     LL limit1;
  20.     LL limit2;
  21.     LL value;
  22.     bool putIntoBag;
  23.  
  24.     ItemInfo(LL limit1, LL limit2, LL value)
  25.         : limit1(limit1),
  26.           limit2(limit2),
  27.           value(value),
  28.           putIntoBag(false) {}
  29.  
  30.     friend ostream &operator<<(ostream &os, const ItemInfo &item);
  31. };
  32.  
  33. vector<ItemInfo> readItemsFromFile()
  34. {
  35.     fstream inputFile;
  36.  
  37.     // open file and check if valid
  38.     inputFile.open("./in.txt", std::ios::in);
  39.     if (!inputFile.is_open())
  40.     {
  41.         cout << "Failed to open file in.txt" << endq;
  42.         exit(-1);
  43.     }
  44.  
  45.     LL itemCount;
  46.     LL limit1, limit2, value;
  47.     vector<ItemInfo> itemsArr;
  48.  
  49.     inputFile >> itemCount;
  50.  
  51.     for (LL i = 0; i < itemCount; ++i)
  52.     {
  53.         inputFile >> limit1 >> limit2 >> value;
  54.         itemsArr.emplace_back(ItemInfo(limit1, limit2, value));
  55.     }
  56.  
  57.     inputFile.close();
  58.  
  59.     return itemsArr;
  60. }
  61.  
  62. ostream &operator<<(ostream &os, const ItemInfo &item)
  63. {
  64.     os << "<ItemInfo limit1:" << item.limit1 << " limit2:" << item.limit2 << " value:" << item.value << ">";
  65.     return os;
  66. }
  67.  
  68. void writeAnswerIntoFile(LL ans)
  69. {
  70.     fstream outputFile;
  71.     outputFile.open("./out.txt", std::ios::out);
  72.     outputFile << ans;
  73.     outputFile.close();
  74. }
  75.  
  76. LL dpArr[10][1000][1000] = {0};
  77. LL calculateMaxValue(vector<ItemInfo> itemArr, LL maxLimit1, LL maxLimit2)
  78. {
  79.     ItemInfo currentItem(0, 0, 0);
  80.     for (LL i = 0; i <= itemArr.size(); ++i)
  81.     {
  82.         for (LL j = 0; j <= maxLimit1; ++j)
  83.         {
  84.             for (LL k = 0; k <= maxLimit2; ++k)
  85.             {
  86.  
  87.                 if (i == 0)
  88.                 {
  89.                     dpArr[i][j][k] = 0;
  90.                     continue;
  91.                 }
  92.  
  93.                 currentItem = itemArr[i - 1];
  94.                 LL valueWhenNotChoosingThis = dpArr[i - 1][j][k];
  95.                 LL valueWhenChoosingThis = dpArr[i - 1][j][k];
  96.                 if (j >= currentItem.limit1 && k >= currentItem.limit2)
  97.                 {
  98.                     valueWhenChoosingThis = dpArr[i - 1][j - currentItem.limit1][k - currentItem.limit2] + currentItem.value;
  99.                 }
  100.                 dpArr[i][j][k] = max(valueWhenChoosingThis, valueWhenNotChoosingThis);
  101.             }
  102.         }
  103.     }
  104.  
  105.     return dpArr[itemArr.size()][maxLimit1][maxLimit2];
  106. }
  107.  
  108. // Check the final situation, determine if an item has been put into the bag
  109. void getSolutionSet(vector<ItemInfo> &items, LL maxLimit1, LL maxLimit2)
  110. {
  111.     LL size = items.size();
  112.     LL curLimit1 = maxLimit1;
  113.     LL curLimit2 = maxLimit2;
  114.  
  115.     cout << "Put into bag: " << endq;
  116.     for (LL currentItem = size; currentItem > 0; --currentItem)
  117.     {
  118.         if (dpArr[currentItem - 1][curLimit1][curLimit2] != dpArr[currentItem][curLimit1][curLimit2])
  119.         {
  120.             cout << items[currentItem - 1] << endq;
  121.             items[currentItem - 1].putIntoBag = true;
  122.             curLimit1 -= items[currentItem - 1].limit1;
  123.             curLimit2 -= items[currentItem - 1].limit2;
  124.         }
  125.     }
  126. }
  127.  
  128. int main()
  129. {
  130.     LL maxLimit1 = 2;
  131.     LL maxLimit2 = 2;
  132.  
  133.     vector<ItemInfo> itemArr = readItemsFromFile();
  134.     // here you can change the max limit 1 and max limit 2
  135.     LL ans = calculateMaxValue(itemArr, maxLimit1, maxLimit2);
  136.     getSolutionSet(itemArr, maxLimit1, maxLimit2);
  137.     writeAnswerIntoFile(ans);
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement