Advertisement
nna42799

Untitled

Dec 13th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 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.  
  23.     ItemInfo(LL limit1, LL limit2, LL value)
  24.         : limit1(limit1),
  25.           limit2(limit2),
  26.           value(value) {}
  27.  
  28.     friend ostream &operator<<(ostream &os, const ItemInfo &item);
  29. };
  30.  
  31. vector<ItemInfo> readItemsFromFile()
  32. {
  33.     fstream inputFile;
  34.  
  35.     // open file and check if valid
  36.     inputFile.open("./in.txt", std::ios::in);
  37.     if (!inputFile.is_open())
  38.     {
  39.         cout << "Failed to open file in.txt" << endq;
  40.         exit(-1);
  41.     }
  42.  
  43.     LL itemCount;
  44.     LL limit1, limit2, value;
  45.     vector<ItemInfo> itemsArr;
  46.  
  47.     inputFile >> itemCount;
  48.  
  49.     for (LL i = 0; i < itemCount; ++i)
  50.     {
  51.         inputFile >> limit1 >> limit2 >> value;
  52.         itemsArr.emplace_back(ItemInfo(limit1, limit2, value));
  53.     }
  54.  
  55.     inputFile.close();
  56.  
  57.     return itemsArr;
  58. }
  59.  
  60. ostream &operator<<(ostream &os, const ItemInfo &item)
  61. {
  62.     os << "<ItemInfo limit1:" << item.limit1 << " limit2:" << item.limit2 << " value:" << item.value << ">";
  63.     return os;
  64. }
  65.  
  66. void writeAnswerIntoFile(LL ans)
  67. {
  68.     fstream outputFile;
  69.     outputFile.open("./out.txt", std::ios::out);
  70.     outputFile << ans;
  71.     outputFile.close();
  72. }
  73.  
  74. LL dpArr[10][1000][1000] = {0};
  75. LL calculateMaxValue(vector<ItemInfo> itemArr, LL maxLimit1, LL maxLimit2)
  76. {
  77.     ItemInfo currentItem(0, 0, 0);
  78.     for (LL i = 0; i <= itemArr.size(); ++i)
  79.     {
  80.         for (LL j = 0; j <= maxLimit1; ++j)
  81.         {
  82.             for (LL k = 0; k <= maxLimit2; ++k)
  83.             {
  84.  
  85.                 if (i == 0)
  86.                 {
  87.                     dpArr[i][j][k] = 0;
  88.                     continue;
  89.                 }
  90.  
  91.                 currentItem = itemArr[i];
  92.                 LL valueWhenNotChoosingThis = dpArr[i - 1][j][k];
  93.                 LL valueWhenChoosingThis = 0;
  94.                 if (j >= currentItem.limit1 && k >= currentItem.limit2)
  95.                 {
  96.                     LL valueWhenChoosingThis = dpArr[i - 1][j - currentItem.limit1][k - currentItem.limit2] + currentItem.value;
  97.                 }
  98.                 dpArr[i][j][k] = max(valueWhenChoosingThis, valueWhenNotChoosingThis);
  99.             }
  100.         }
  101.     }
  102.  
  103.     return dpArr[itemArr.size()][maxLimit1][maxLimit2];
  104. }
  105.  
  106. int main()
  107. {
  108.     vector<ItemInfo> itemArr = readItemsFromFile();
  109.     // here you can change the max limit 1 and max limit 2
  110.     LL ans = calculateMaxValue(itemArr, 50, 50);
  111.     writeAnswerIntoFile(ans);
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement