Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long int ll;
- int d[102][1010];
- int p[102][1010];
- vector<int> o[102][1010];
- int ti[102];
- int pr[102];
- int main() {
- int n, tmax;
- cin >> n >> tmax;
- for (int i = 0; i < n; ++i)
- cin >> ti[i] >> pr[i];
- for (int i = 1; i <= tmax; ++i) {
- for (int j = 0; j < n; ++j) {
- cerr << "d[" << j << "][" << i << "]\n";
- //cerr << "case 1\n";
- if (j == 0) {
- if (i >= ti[j]) {
- d[j][i] = pr[0];
- p[j][i] = i * pr[0];
- o[j][i].push_back(0);
- }
- continue;
- }
- //cerr << "we're here\n";
- d[j][i] = d[j - 1][i];
- p[j][i] = p[j - 1][i];
- o[j][i] = o[j - 1][i];
- if (i - ti[j] >= 0) {
- //cerr << "case 2\n";
- if (d[j - 1][i - ti[j]] + pr[j] >= d[j][i]) {
- int alt_probs = d[j - 1][i - ti[j]] + pr[j];
- vector<int> alt_op = o[j - 1][i - ti[j]];
- int alt_pen;
- if (alt_op.empty()) {
- alt_op.push_back(j);
- alt_pen = i * pr[j];
- cerr << "alt_pen = " << alt_pen << endl;
- } else {
- vector<int> pen_sum(alt_op.size() + 3);
- pen_sum[alt_op.size() - 1] = pr[alt_op.back()];
- for (int k = ((int)alt_op.size()) - 2; k >= 0; --k) {
- pen_sum[k] = pen_sum[k + 1] + pr[alt_op[k]];
- }
- vector<int> t_sum {0};
- for (int i = 0; i < ((int)alt_op.size()); ++i)
- t_sum.push_back(t_sum.back() + ti[alt_op[i]]);
- pair<int, int> penalty(std::numeric_limits<int>::max(), -1);
- for (int k = 0; k <= static_cast<int>(alt_op.size()); ++k) {
- pair<int, int> alt(pr[j] * (t_sum[k] + ti[j]) + pen_sum[k + 1] * ti[j], k);
- if (alt < penalty)
- penalty = alt;
- }
- alt_pen = penalty.first;
- cerr << "alt_pen = " << alt_pen << endl;
- alt_op.insert(alt_op.begin() + penalty.second, j);
- }
- if (alt_probs > d[j][i] || alt_pen < p[j][i]) {
- d[j][i] = alt_probs;
- p[j][i] = alt_pen;
- o[j][i] = alt_op;
- }
- }
- }
- cout << "size = " << o[i][j].size() << '\n';
- for (int x : o[i][j]) {
- cout << x + 1 << ' ';
- }
- }
- }
- //cerr << d[n - 1][tmax] << endl;
- cout << o[n - 1][tmax].size() << '\n';
- for (int x : o[n - 1][tmax]) {
- cout << x + 1 << ' ';
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement