Advertisement
AmidamaruZXC

Untitled

Dec 7th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <sstream>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int max(int a, int b) { return (a > b) ? a : b; }
  10.  
  11. bool isIntNumb(string s) {
  12.     return s.find_first_not_of("0123456789") == string::npos;
  13. }
  14.  
  15. int NOD(int x, int y)
  16. {
  17.     return !y ? x : NOD(y, x % y);
  18. }
  19.  
  20. int NOD(vector<int> numbers)
  21. {
  22.     if (numbers.empty())
  23.         return 1;
  24.  
  25.     if (numbers.size() == 1)
  26.         return numbers[0];
  27.  
  28.     int res = numbers[0];
  29.  
  30.     for (int i = 1; i < numbers.size(); i++)
  31.         res = NOD(res, numbers[i]);
  32.  
  33.     return res;
  34. }
  35.  
  36. vector<int> Solve(vector<int> weight, vector<int> cost, int& maxC, int& maxW, int w)
  37. {
  38.     int nod = NOD(NOD(weight), w);
  39.     w /= nod;
  40.     for (int i = 0; i < weight.size(); i++)
  41.         weight[i] /= nod;
  42.  
  43.     int maxWeight = 0;
  44.  
  45.     for (int i = 0; i < weight.size(); ++i)
  46.         maxWeight += weight[i];
  47.     if (w < maxWeight)
  48.         maxWeight = w;
  49.  
  50.     int width = maxWeight + 1;
  51.     int height = cost.size() + 1;
  52.  
  53.     vector<vector<int>> matrix(height, vector<int>(width));
  54.     for (int i = 0; i < height; ++i)
  55.         matrix[i][0] = 0;
  56.     for (int i = 0; i < width; ++i)
  57.         matrix[0][i] = 0;
  58.  
  59.  
  60.     for (int i = 0; i < height - 1; ++i)
  61.         for (int j = 0; j < width; ++j)
  62.             if (j >= weight[i])
  63.             {
  64.                 if (matrix[i][j] > matrix[i][j - weight[i]] + cost[i])
  65.                     matrix[i + 1][j] = matrix[i][j];
  66.                 else
  67.                     matrix[i + 1][j] = matrix[i][j - weight[i]] + cost[i];
  68.             }
  69.             else
  70.                 matrix[i + 1][j] = matrix[i][j];
  71.  
  72.     vector<int> answer;
  73.     int tempCost = matrix[height - 1][width - 1];
  74.     int tempW = maxWeight;
  75.     for (int i = height - 1; i > 0; --i)
  76.         if (matrix[i][tempW] > matrix[i - 1][tempW]) {
  77.             answer.push_back(i);
  78.             maxC += cost[i - 1];
  79.             maxW += weight[i - 1] * nod;
  80.             tempCost -= cost[i - 1];
  81.             tempW -= weight[i - 1];
  82.         }
  83.  
  84.     return answer;
  85. }
  86.  
  87. int main()
  88. {
  89.     int a, b, volume, errorsCount = 0;
  90.     vector<string> input;
  91.     string str, numb;
  92.     getline(cin, str);
  93.     stringstream in(str);
  94.     while (in >> numb)
  95.         input.push_back(numb);
  96.     volume = stoi(input[0]);
  97.     input.clear();
  98.     vector<int> weight;
  99.     vector<int> cost;
  100.     while (!cin.fail()) {
  101.         getline(cin, str);
  102.         stringstream in(str);
  103.         while (in >> numb)
  104.             input.push_back(numb);
  105.         if (input.size() == 0)
  106.         {
  107.             input.clear();
  108.             continue;
  109.         }
  110.         else if (input.size() == 1 || input.size() > 2)
  111.         {
  112.             errorsCount++;
  113.             input.clear();
  114.             continue;
  115.         }
  116.         else if (!isIntNumb(input[0]) || !isIntNumb(input[1]))
  117.         {
  118.             errorsCount++;
  119.             input.clear();
  120.             continue;
  121.         }
  122.         a = stoi(input[0]);
  123.         b = stoi(input[1]);
  124.         input.clear();
  125.         if (a >= 0 && b >= 0)
  126.         {
  127.             weight.push_back(a);
  128.             cost.push_back(b);
  129.         }
  130.         else
  131.             errorsCount++;
  132.     }
  133.     int maxW = 0;
  134.     int maxC = 0;
  135.  
  136.     vector<int> matrix = Solve(weight, cost, maxC, maxW, volume);
  137.     if (matrix.empty())
  138.     {
  139.         cout << "0 0" << endl;
  140.         return 0;
  141.     }
  142.  
  143.     for (int i = 0; i < errorsCount; ++i)
  144.         cout << "error" << endl;
  145.     cout << maxW << " " << maxC << endl;
  146.     for (int i = matrix.size() - 1; i >= 0; --i) {
  147.         cout << matrix[i] << endl;
  148.     }
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement