Advertisement
AmidamaruZXC

TaskA

Dec 4th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 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.  
  16. vector<int> Solve(vector<int> weight, vector<int> cost, int& maxC, int& maxW, int w)
  17. {
  18.     int maxWeight = 0;
  19.  
  20.     for (int i = 0; i < weight.size(); ++i)
  21.         maxWeight += weight[i];
  22.     if (w < maxWeight)
  23.         maxWeight = w;
  24.  
  25.     int width = maxWeight + 1;
  26.     int height = cost.size() + 1;
  27.  
  28.     vector<vector<int>> matrix(height, vector<int>(width));
  29.     for (int i = 0; i < height; ++i)
  30.         matrix[i][0] = 0;
  31.     for (int i = 0; i < width; ++i)
  32.         matrix[0][i] = 0;
  33.  
  34.  
  35.     for (int i = 0; i < height - 1; ++i)
  36.         for (int j = 0; j < width; ++j)
  37.             if (j >= weight[i])
  38.             {
  39.                 if (matrix[i][j] > matrix[i][j - weight[i]] + cost[i])
  40.                     matrix[i + 1][j] = matrix[i][j];
  41.                 else
  42.                     matrix[i + 1][j] = matrix[i][j - weight[i]] + cost[i];
  43.             }
  44.             else
  45.                 matrix[i + 1][j] = matrix[i][j];
  46.  
  47.     vector<int> answer;
  48.     int tempCost = matrix[height - 1][width - 1];
  49.     int tempW = maxWeight;
  50.     for (int i = height - 1; i > 0; --i)
  51.         if (matrix[i][tempW] > matrix[i - 1][tempW]) {
  52.             answer.push_back(i);
  53.             maxC += cost[i - 1];
  54.             maxW += weight[i - 1];
  55.             tempCost -= cost[i - 1];
  56.             tempW -= weight[i - 1];
  57.         }
  58.  
  59.     return answer;
  60. }
  61.  
  62. int main()
  63. {
  64.     int a, b, volume, errorsCount = 0;
  65.     vector<string> input;
  66.     string str, numb;
  67.     getline(cin, str);
  68.     stringstream in(str);
  69.     while (in >> numb)
  70.         input.push_back(numb);
  71.     volume = stoi(input[0]);
  72.     input.clear();
  73.     vector<int> weight;
  74.     vector<int> cost;
  75.     while (!cin.fail()) {
  76.         getline(cin, str);
  77.         stringstream in(str);
  78.         while (in >> numb)
  79.             input.push_back(numb);
  80.         if (input.size() == 0)
  81.         {
  82.             input.clear();
  83.             continue;
  84.         }
  85.         else if (input.size() == 1 || input.size() > 2)
  86.         {
  87.             errorsCount++;
  88.             input.clear();
  89.             continue;
  90.         }
  91.         else if (!isIntNumb(input[0]) || !isIntNumb(input[1]))
  92.         {
  93.             errorsCount++;
  94.             input.clear();
  95.             continue;
  96.         }
  97.         a = stoi(input[0]);
  98.         b = stoi(input[1]);
  99.         input.clear();
  100.         if (a >= 0 && b >= 0)
  101.         {
  102.             weight.push_back(a);
  103.             cost.push_back(b);
  104.         }
  105.         else
  106.             errorsCount++;
  107.     }
  108.     int maxW = 0;
  109.     int maxC = 0;
  110.  
  111.     vector<int> matrix = Solve(weight, cost, maxC, maxW, volume);
  112.     if (matrix.empty())
  113.     {
  114.         cout << "0 0" << endl;
  115.         return 0;
  116.     }
  117.  
  118.     for (int i = 0; i < errorsCount; ++i)
  119.         cout << "error" << endl;
  120.     cout << maxW << " " << maxC << endl;
  121.     for (int i = matrix.size() - 1; i >= 0; --i) {
  122.         cout << matrix[i] << endl;
  123.     }
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement