Advertisement
Korotkodul

швб_21-22_N5 > 1h

Oct 22nd, 2022 (edited)
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int, int>
  11. #define pb(x) push_back(x)
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v) {
  17.     for (auto x : v) cout << x << ' ';
  18.     cout << "\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v) {
  22.     for (auto x : v) cout << x << ' ';
  23.     cout << "\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v) {
  28.     for (auto x : v) cv(x);
  29.     cout << "\n";
  30. }
  31.  
  32. void cvb(vector <bool> v) {
  33.     for (bool x : v) cout << x << ' ';
  34.     cout << "\n";
  35. }
  36.  
  37. void cvs(vector <string>  v) {
  38.     for (auto a : v) {
  39.         cout << a << "\n";
  40.     }
  41. }
  42.  
  43. void cvp(vector <pii> a) {
  44.     for (auto p : a) {
  45.         cout << p.first << ' ' << p.second << "\n";
  46.     }
  47.     cout << "\n";
  48. }
  49.  
  50. bool sh = 0;
  51.  
  52. bool cmp(vector <int> a, vector <int> b) {
  53.     return a[0] < b[0];
  54. }
  55.  
  56.  
  57. int main() {
  58.     /*ios::sync_with_stdio(0);
  59.     cin.tie(0);
  60.     cout.tie(0);*/
  61.     int n, goal, m = 0;
  62.     cin >> n;
  63.     vector <int> pr(n + 1), w(n + 1);
  64.     for (int i = 1; i <= n; ++i) {
  65.         int x, y, z;
  66.         cin >> x >> y >> z;
  67.         pr[i] = x;
  68.         w[i] = y * z / 100;
  69.         m += w[i];
  70.     }
  71.     cin >> goal;
  72.     vector <vector <int> > cost(m + 1, vector <int> (n + 1, 0));
  73.     vector <vector <bool> > can(m + 1, vector <bool> (n + 1, 0));
  74.     vector <vector <pii> > pred(m + 1, vector <pii> (n + 1, {-1, -1}));
  75.     for (int i = 0; i <= n; ++i) {
  76.         can[0][i] = 1;
  77.     }
  78.     for (int i = 1; i <= m; ++i) {
  79.         for (int j = 1; j <= n; ++j) {
  80.             can[i][j] = can[i][j - 1];
  81.             pred[i][j] = pred[i][j - 1];
  82.             cost[i][j] = cost[i][j - 1];
  83.  
  84.             if (i - w[j] < 0) {
  85.                 continue;
  86.             }
  87.             if (!can[i - w[j]][j - 1]) {
  88.                 continue;
  89.             }
  90.             can[i][j] = 1;
  91.  
  92.             bool A = !can[i][j - 1];
  93.             bool B = cost[i - w[j]][j - 1] + pr[j] < cost[i][j - 1];
  94.             if (A || B) {
  95.                 cost[i][j] = cost[i - w[j]][j - 1] + pr[j];
  96.                 pred[i][j] = {i - w[j], j - 1};
  97.             }
  98.         }
  99.     }
  100.     if (sh) {
  101.         cout << "res\n";
  102.         cout << "cost\n";
  103.         cout << "sz = " << cost.size() << "\n";
  104.         cvv(cost);
  105.         cout << "\npred\n";
  106.         for (auto v: pred) {
  107.             for (pii p: v) {
  108.                 cout << "(" << p.first << "," << p.second << ") ";
  109.             } cout << "\n";
  110.         } cout << "\n";
  111.     }
  112.     vector <vector <int> > bst; // {num, id of raw, id of column}
  113.     for (int i = goal; i <= m; ++i) {
  114.         if (can[i][n]) {
  115.             int id = n;
  116.             while (can[i][id - 1] && cost[i][id - 1] == cost[i][n]) {
  117.                 id--;
  118.             }
  119.             bst.push_back({cost[i][n], i, id});
  120.         }
  121.     }
  122.     sort(bst.begin(), bst.end(), cmp);
  123.     pii p = {bst[0][1], bst[0][2]};
  124.     vector <int> ans;
  125.     if (sh) {
  126.         cout << "bst\n";
  127.         for (auto v: bst) {
  128.             cv(v);
  129.         }
  130.     }
  131.     while (p.first != 0 && p != pii{-1, -1}) {
  132.         if (sh) {
  133.             cout << "p = " << p.first << " " << p.second << "\n";
  134.         }
  135.         ans.push_back(p.second);
  136.         p = pred[p.first][p.second];
  137.     }
  138.     sort(ans.begin(), ans.end());
  139.     cv(ans);
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement