Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int, int>
- #define pb(x) push_back(x)
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvl(vector <ll> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvv(vector <vector <int> > &v) {
- for (auto x : v) cv(x);
- cout << "\n";
- }
- void cvb(vector <bool> v) {
- for (bool x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvs(vector <string> v) {
- for (auto a : v) {
- cout << a << "\n";
- }
- }
- void cvp(vector <pii> a) {
- for (auto p : a) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- bool sh = 0;
- bool cmp(vector <int> a, vector <int> b) {
- return a[0] < b[0];
- }
- int main() {
- /*ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);*/
- int n, goal, m = 0;
- cin >> n;
- vector <int> pr(n + 1), w(n + 1);
- for (int i = 1; i <= n; ++i) {
- int x, y, z;
- cin >> x >> y >> z;
- pr[i] = x;
- w[i] = y * z / 100;
- m += w[i];
- }
- cin >> goal;
- vector <vector <int> > cost(m + 1, vector <int> (n + 1, 0));
- vector <vector <bool> > can(m + 1, vector <bool> (n + 1, 0));
- vector <vector <pii> > pred(m + 1, vector <pii> (n + 1, {-1, -1}));
- for (int i = 0; i <= n; ++i) {
- can[0][i] = 1;
- }
- for (int i = 1; i <= m; ++i) {
- for (int j = 1; j <= n; ++j) {
- can[i][j] = can[i][j - 1];
- pred[i][j] = pred[i][j - 1];
- cost[i][j] = cost[i][j - 1];
- if (i - w[j] < 0) {
- continue;
- }
- if (!can[i - w[j]][j - 1]) {
- continue;
- }
- can[i][j] = 1;
- bool A = !can[i][j - 1];
- bool B = cost[i - w[j]][j - 1] + pr[j] < cost[i][j - 1];
- if (A || B) {
- cost[i][j] = cost[i - w[j]][j - 1] + pr[j];
- pred[i][j] = {i - w[j], j - 1};
- }
- }
- }
- if (sh) {
- cout << "res\n";
- cout << "cost\n";
- cout << "sz = " << cost.size() << "\n";
- cvv(cost);
- cout << "\npred\n";
- for (auto v: pred) {
- for (pii p: v) {
- cout << "(" << p.first << "," << p.second << ") ";
- } cout << "\n";
- } cout << "\n";
- }
- vector <vector <int> > bst; // {num, id of raw, id of column}
- for (int i = goal; i <= m; ++i) {
- if (can[i][n]) {
- int id = n;
- while (can[i][id - 1] && cost[i][id - 1] == cost[i][n]) {
- id--;
- }
- bst.push_back({cost[i][n], i, id});
- }
- }
- sort(bst.begin(), bst.end(), cmp);
- pii p = {bst[0][1], bst[0][2]};
- vector <int> ans;
- if (sh) {
- cout << "bst\n";
- for (auto v: bst) {
- cv(v);
- }
- }
- while (p.first != 0 && p != pii{-1, -1}) {
- if (sh) {
- cout << "p = " << p.first << " " << p.second << "\n";
- }
- ans.push_back(p.second);
- p = pred[p.first][p.second];
- }
- sort(ans.begin(), ans.end());
- cv(ans);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement