# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1040216 | BackNoob | Salesman (IOI09_salesman) | C++14 | 611 ms | 37572 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = (ll) 1e13 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
struct Segtree{
int n;
vector<ll> t;
Segtree(){}
Segtree(int _n) {
n = _n;
int offset = 1;
while(offset <= n) offset *= 2;
t.resize(offset * 2 + 7, -infll);
}
void update(int v, int tl, int tr, int pos, ll val) {
if(tl == tr) t[v] = val;
else {
int tm = (tl + tr) / 2;
if(pos <= tm) update(v * 2, tl, tm, pos, val);
else update(v * 2 + 1, tm + 1, tr, pos, val);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
}
ll getmax(int v, int tl, int tr, int l, int r) {
if(l > r) return -infll;
if(tl == l && tr == r) return t[v];
int tm = (tl + tr) / 2;
ll m1 = getmax(v * 2, tl, tm, l, min(r, tm));
ll m2 = getmax(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
return max(m1, m2);
}
void update(int pos, ll val) {
update(1, 1, n, pos, val);
}
ll getmax(int l, int r) {
return getmax(1, 1, n, l, r);
}
} segl, segr;
struct item{
int t, p, c;
bool operator<(const item &other) const {
if(t == other.t) return p < other.p;
return t < other.t;
}
} a[mxN];
int n;
int down;
int up;
int home;
ll dp[mxN];
void solve()
{
cin >> n >> down >> up >> home;
for(int i = 1; i <= n; i++) cin >> a[i].t >> a[i].p >> a[i].c;
sort(a + 1, a + 1 + n);
vector<int> val;
val.pb(home);
for(int i = 1; i <= n; i++) val.pb(a[i].p);
sort(all(val));
val.erase(unique(all(val)), val.end());
segl = Segtree(sz(val) + 7);
segr = Segtree(sz(val) + 7);
for(int i = 0; i <= n; i++) dp[i] = -infll;
int pos = lower_bound(all(val), home) - val.begin() + 1;
segl.update(pos, home * up);
segr.update(pos, -home * down);
for(int i = 1; i <= n; i++) {
int j = i;
while(j <= n && a[i].t == a[j].t) ++j;
ll curmax = dp[0];
ll curmax2 = dp[0];
ll pre = dp[0];
for(int k = i; k < j; k++) {
int pos = lower_bound(all(val), a[k].p) - val.begin() + 1;
maximize(curmax, segl.getmax(1, pos - 1) - 1LL * a[k].p * up);
if(k != i) {
int lef = lower_bound(all(val), a[k - 1].p) - val.begin() + 1;
maximize(curmax2, segr.getmax(lef, pos - 1) + pre - 1LL * a[k].p * up);
}
curmax += a[k].c;
curmax2 += a[k].c;
pre += a[k].c;
maximize(dp[k], curmax);
maximize(dp[k], curmax2);
maximize(pre, 1LL * a[k].p * down + 1LL * a[k].p * up + a[k].c);
if(k + 1 <= j) {
curmax -= (a[k + 1].p - a[k].p) * up;
curmax2 -= (a[k + 1].p - a[k].p) * up;
}
}
curmax = dp[0];
curmax2 = dp[0];
pre = dp[0];
for(int k = j - 1; k >= i; k--) {
int pos = lower_bound(all(val), a[k].p) - val.begin() + 1;
maximize(curmax, segr.getmax(pos + 1, sz(val)) + 1LL * a[k].p * down);
if(k != j - 1) {
int rig = lower_bound(all(val), a[k + 1].p) - val.begin() + 1;
maximize(curmax2, segl.getmax(pos + 1, rig) + pre + 1LL * a[k].p * down);
}
curmax += a[k].c;
curmax2 += a[k].c;
pre += a[k].c;
maximize(dp[k], curmax);
maximize(dp[k], curmax2);
maximize(pre, - 1LL * a[k].p * down - 1LL * a[k].p * up + a[k].c);
if(k - 1 >= i) {
curmax -= (a[k].p - a[k - 1].p) * down;
curmax2 -= (a[k].p - a[k - 1].p) * down;
}
}
for(int k = i; k < j; k++) {
int pos = lower_bound(all(val), a[k].p) - val.begin() + 1;
segl.update(pos, dp[k] + 1LL * a[k].p * up);
segr.update(pos, dp[k] - 1LL * a[k].p * down);
}
i = j - 1;
// cout << dp[i] << ' ';
}
ll res = 0;
for(int i = 1; i <= n; i++) {
if(a[i].p <= home) maximize(res, dp[i] - 1LL * (home - a[i].p) * up);
else maximize(res, dp[i] - 1LL * (a[i].p - home) * down);
}
cout << res << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |