Hướng dẫn giải của Máy in tiền
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
#include <bits/stdc++.h> using namespace std; namespace std { #ifndef LOCAL #define cerr \ if (0) cerr #endif } // namespace std #define int int64_t struct Item { int sell_day; int sell_price; int get_price; int gain_amount; Item(int a = 0, int b = 0, int c = 0, int d = 0) : sell_day(a), sell_price(b), get_price(c), gain_amount(d) {} bool operator<(const Item& rhs) const { return sell_day < rhs.sell_day; } }; istream& operator>>(istream& ist, Item& i) { ist >> i.sell_day >> i.sell_price >> i.get_price >> i.gain_amount; return ist; } #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; struct Line { mutable ll k, m, p; bool operator<(const Line& o) const { return k < o.k; } bool operator<(ll x) const { return p < x; } }; struct LineContainer : multiset<Line, less<>> { // (for doubles, use inf = 1/.0, div(a,b) = a/b) static const ll inf = LLONG_MAX; ll div(ll a, ll b) { // floored division return a / b - ((a ^ b) < 0 && a % b); } bool isect(iterator x, iterator y) { if (y == end()) return x->p = inf, 0; if (x->k == y->k) x->p = x->m > y->m ? inf : -inf; else x->p = div(y->m - x->m, x->k - y->k); return x->p >= y->p; } void add(ll k, ll m) { auto z = insert({k, m, 0}), y = z++, x = y; while (isect(y, z)) z = erase(z); if (x != begin() && isect(--x, y)) isect(x, y = erase(y)); while ((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y)); } ll query(ll x) { assert(!empty()); auto l = *lower_bound(x); return l.k * x + l.m; } }; Item a[100005]; int64_t dp[100005]; int trace[100005]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifdef LOCAL #define task "a" #else #define task "" #endif if (fopen(task ".inp", "r")) { freopen(task ".inp", "r", stdin); freopen(task ".out", "w", stdout); } int num_item, first_amount, max_day; cin >> num_item >> first_amount >> max_day; for (int i = 1; i <= num_item; i++) { cin >> a[i]; } a[++num_item] = Item(max_day + 1); sort(a + 1, a + num_item + 1); memset(dp, -0x3f, sizeof(dp)); dp[0] = first_amount; LineContainer cvh; cvh.add(0, dp[0]); for (int i = 1; i <= num_item; i++) { dp[i] = cvh.query(a[i].sell_day) - a[i].sell_price; if (dp[i] >= 0) cvh.add(a[i].gain_amount, dp[i] - 1ll * (a[i].sell_day + 1) * a[i].gain_amount + a[i].get_price); } cout << dp[num_item]; return 0; }
Bình luận