# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
659288 | marvinthang | Salesman (IOI09_salesman) | C++17 | 723 ms | 51532 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.
/*************************************
* author: marvinthang *
* created: 17.11.2022 15:31:48 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define div ___div
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define FULL(i) (MASK(i) - 1)
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define FORE(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class T> print_op(vector <T>) { out << '{'; for (size_t i = 0; i + 1 < u.size(); ++i) out << u[i] << ", "; if (!u.empty()) out << u.back(); return out << '}'; }
template <class U, class V> scan_op(pair <U, V>) { return in >> u.fi >> u.se; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.fi << ", " << u.se << ')'; }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
// end of template
const double PI = 3.1415926535897932384626433832795; // acos(-1.0); atan(-1.0);
const int dir[] = {1, 0, -1, 0, 1, 1, -1, -1, 1}; // {2, 1, -2, -1, -2, 1, 2, -1, 2};
const int MAX = 5e5 + 5;
const int INF = 2e9 + 7;
struct TradeFair {
int day, position, profitability;
TradeFair(int d = 0, int l = 0, int p = 0): day(d), position(l), profitability(p) {}
};
int numTradeFair, upStreamCost, downStreamCost, homePos;
TradeFair tf[MAX];
vector <int> inDay[MAX];
void init(void) {
cin >> numTradeFair >> upStreamCost >> downStreamCost >> homePos;
FOR(i, 1, numTradeFair) {
cin >> tf[i].day >> tf[i].position >> tf[i].profitability;
inDay[tf[i].day].push_back(i);
}
}
struct Node {
int maxUp, maxDown;
Node(): maxUp(-INF),maxDown(-INF) {}
Node(int dp, int pos) {
maxUp = dp - pos * upStreamCost;
maxDown = dp + pos * downStreamCost;
}
Node operator + (const Node &other) const {
Node res;
res.maxUp = max(maxUp, other.maxUp);
res.maxDown = max(maxDown, other.maxDown);
return res;
}
} st[MAX << 2];
void update(int i, int l, int r, int p, int dp) {
if (l == r) {
st[i] = Node(dp, l);
return;
}
int m = l + r >> 1;
if (p <= m) update(i << 1, l, m, p, dp);
else update(i << 1 | 1, m + 1, r, p, dp);
st[i] = st[i << 1] + st[i << 1 | 1];
}
Node get(int i, int l, int r, int u, int v) {
if (l > v || r < u) return Node();
if (u <= l && r <= v) return st[i];
int m = l + r >> 1;
return get(i << 1, l, m, u, v) + get(i << 1 | 1, m + 1, r, u, v);
}
int dp[MAX];
void process(void) {
int maxPosition = homePos, maxDay = 0;
FOR(i, 1, numTradeFair) {
maximize(maxPosition, tf[i].position);
maximize(maxDay, tf[i].day);
}
tf[++numTradeFair] = TradeFair(++maxDay, homePos, 0);
inDay[maxDay].push_back(numTradeFair);
update(1, 1, maxPosition, homePos, 0);
FOR(d, 1, maxDay) if (!inDay[d].empty()) {
for (int i: inDay[d]) {
dp[i] = get(1, 1, maxPosition, 1, tf[i].position).maxDown - tf[i].position * downStreamCost;
maximize(dp[i], get(1, 1, maxPosition, tf[i].position, maxPosition).maxUp + tf[i].position * upStreamCost);
dp[i] += tf[i].profitability;
}
sort(inDay[d].begin(), inDay[d].end(), [&] (int i, int j) { return tf[i].position < tf[j].position; });
vector <int> f(inDay[d].size(), -INF), g(inDay[d].size(), -INF);
int best = -INF;
REP(p, inDay[d].size()) {
int i = inDay[d][p];
best = max(best, tf[i].position * (downStreamCost + upStreamCost));
f[p] = best + dp[i] - tf[i].position * upStreamCost;
best += tf[i].profitability;
}
best = -INF;
FORD(p, (int) inDay[d].size() - 1, 0) {
int i = inDay[d][p];
best = max(best, tf[i].position * (-upStreamCost - downStreamCost));
g[p] = best + dp[i] + tf[i].position * upStreamCost;
best += tf[i].profitability;
}
best = -INF;
REP(p, inDay[d].size()) {
int i = inDay[d][p];
maximize(dp[i], best - tf[i].position * downStreamCost);
maximize(best, f[p]);
}
best = -INF;
FORD(p, (int) inDay[d].size() - 1, 0) {
int i = inDay[d][p];
maximize(dp[i], best + tf[i].position * upStreamCost);
update(1, 1, maxPosition, tf[i].position, dp[i]);
maximize(best, g[p]);
}
}
cout << dp[numTradeFair] << '\n';
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
file("salesman");
init();
process();
cerr << "Time elapsed: " << TIME << " s.\n";
return (0^0);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |