Advertisement
AmidamaruZXC

Untitled

Nov 30th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <sstream>
  4. #include <iostream>
  5.  
  6.  
  7. using std::cout;
  8. using std::string;
  9. using std::cin;
  10. using std::endl;
  11. using std::vector;
  12. using std::pair;
  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> xs)
  21. {
  22.     if (xs.empty()) return 1;
  23.     if (xs.size() == 1) return xs[0];
  24.     int res = xs[0];
  25.     for (int i = 1; i < xs.size(); i++)
  26.         res = NOD(res, xs[i]);
  27.     return res;
  28. }
  29.  
  30.  
  31. vector<int> packed(vector<int> mass, vector<int> cost, int bag_vol)
  32. {
  33.     int max_mass = 0;
  34.     for (int i = 0; i < mass.size(); i++)
  35.         max_mass += mass[i];
  36.     if (bag_vol < max_mass)
  37.         max_mass = bag_vol;
  38.     int width = max_mass + 1;
  39.     int height = cost.size() + 1;
  40.     vector<vector<int>> result;
  41.     for (int i = 0; i < height; i++) {
  42.         result.push_back(vector<int>());
  43.         for (int j = 0; j < width; j++) {
  44.             result[i].push_back(0);
  45.         }
  46.     }
  47.     for (int i = 0; i < width; i++)
  48.         result[0][i] = 0;
  49.  
  50.     for (int i = 0; i < height - 1; i++) {
  51.         int c_i = cost[i];
  52.         int m_i = mass[i];
  53.         for (int j = 0; j < width; j++) {
  54.             if (j >= m_i) {
  55.                 if (result[i][j] > result[i][j - m_i] + c_i)
  56.                     result[i + 1][j] = result[i][j];
  57.                 else
  58.                     result[i + 1][j] = result[i][j - m_i] + c_i;
  59.             }
  60.             else {
  61.                 result[i + 1][j] = result[i][j];
  62.             }
  63.         }
  64.     }
  65.  
  66.     vector<int> taken;
  67.     int left_cost = result[height - 1][width - 1];
  68.     int left_m = max_mass;
  69.     for (int i = height - 1; i > 0; i--) {
  70.         if (result[i][left_m] > result[i - 1][left_m]) {
  71.             taken.push_back(i);
  72.             left_cost -= cost[i - 1];
  73.             left_m -= mass[i - 1];
  74.         }
  75.     }
  76.     return taken;
  77. }
  78.  
  79. bool isInteger(const string s) {
  80.     return s.find_first_not_of("0123456789") == string::npos;
  81. }
  82.  
  83. void run_tests()
  84. {
  85.     int t1, t2, volume;
  86.     vector<string> words;
  87.     string str, word;
  88.     getline(cin, str);
  89.     std::stringstream in(str);
  90.     while (in >> word)
  91.         words.push_back(word);
  92.     volume = stoi(words[0]);
  93.     words.clear();
  94.     vector<int> mass;
  95.     vector<int> cost;
  96.     while (!cin.fail()) {
  97.         getline(cin, str);
  98.         std::stringstream in(str);
  99.         while (in >> word)
  100.             words.push_back(word);
  101.         if (words.size() == 0)
  102.         {
  103.             words.clear();
  104.             continue;
  105.         }
  106.         if (words.size() == 1 || words.size() > 2)
  107.         {
  108.             cout << "error" << endl;
  109.             words.clear();
  110.             continue;
  111.         }
  112.         if (!isInteger(words[0]) || !isInteger(words[1]))
  113.         {
  114.             cout << "error" << endl;
  115.             words.clear();
  116.             continue;
  117.         }
  118.         t1 = stoi(words[0]);
  119.         t2 = stoi(words[1]);
  120.         words.clear();
  121.         if (t1 >= 0 && t2 >= 0)
  122.         {
  123.             mass.push_back(t1);
  124.             cost.push_back(t2);
  125.         }
  126.         else
  127.             cout << "error" << endl;
  128.     }
  129.     int nod = NOD(NOD(mass), volume);
  130.     volume /= nod;
  131.     for (int i = 0; i < mass.size(); i++)
  132.         mass[i] /= nod;
  133.  
  134.     vector<int> objects = packed(mass, cost, volume);
  135.     if (objects.empty())
  136.     {
  137.         cout << "0 0" << endl;
  138.         return;
  139.     }
  140.  
  141.     int sum_m = 0;
  142.     int sum_c = 0;
  143.     for (int i = 0; i < objects.size(); i++) {
  144.         sum_m += mass[objects[i] - 1];
  145.         sum_c += cost[objects[i] - 1];
  146.     }
  147.     sum_m *= nod;
  148.     cout << sum_m << " " << sum_c << endl;
  149.     if (objects.empty())
  150.     {
  151.         cout << "0 0" << endl;
  152.         return;
  153.     }
  154.     for (int i = objects.size() - 1; i >= 0; i--) {
  155.         cout << objects[i] << endl;
  156.     }
  157. }
  158.  
  159.  
  160. int main()
  161. {
  162.     run_tests();
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement