/*
ID: varunra2
LANG: C++
TASK: salesman
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
int n, u, d, s;
VVI fairs;
void init() { fairs.resize(n); }
const int mx = 500005;
struct segtree {
VI st;
VI lzy;
int n;
void init(int _n = 0) {
_n = mx;
n = _n;
st.assign(4 * n, -INF);
lzy.assign(4 * n, -INF);
}
void push(int node, int l, int r) {
st[node] = max(st[node], lzy[node]);
if (l < r) {
lzy[2 * node + 1] = max(lzy[2 * node + 1], lzy[node]);
lzy[2 * node + 2] = max(lzy[2 * node + 2], lzy[node]);
}
lzy[node] = -INF;
}
int qry(int ind, int node, int l, int r) {
if (ind < l or ind > r) return -INF;
push(node, l, r);
if (ind == l and ind == r) return st[node];
int mid = (l + r) / 2;
return max(qry(ind, 2 * node + 1, l, mid),
qry(ind, 2 * node + 2, mid + 1, r));
}
void upd(int tl, int tr, int val, int node, int l, int r) {
if (tl > r or tr < l) return;
push(node, l, r);
if (l >= tl and r <= tr) {
lzy[node] = max(lzy[node], val);
push(node, l, r);
return;
}
int mid = (l + r) / 2;
upd(tl, tr, val, 2 * node + 1, l, mid);
upd(tl, tr, val, 2 * node + 2, mid + 1, r);
st[node] = max(st[2 * node + 1], st[2 * node + 2]);
}
int qry(int ind) { return qry(ind, 0, 0, n - 1); }
void upd(int l, int r, int val) {
if (l == -1) l = 0;
if (r == -1) r = n - 1;
r = min(r, n - 1);
l = min(l, n - 1);
upd(l, r, val, 0, 0, n - 1);
}
} lft, rt;
int main() {
// #ifndef ONLINE_JUDGE
// freopen("salesman.in", "r", stdin);
// freopen("salesman.out", "w", stdout);
// #endif
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n >> u >> d >> s;
init();
lft.init();
rt.init();
for (int i = 0; i < n; i++) {
fairs[i].resize(3);
for (int j = 0; j < 3; j++) cin >> fairs[i][j];
}
sort(all(fairs));
lft.upd(s, -1, s * d);
rt.upd(-1, s, -s * u);
// debug(lft.qry(0), s * d);
VI dp(n, -INF);
VI ndp(n, -INF);
for (int i = 0, j = 0; i < n; i = j) {
while (j < n and fairs[i][0] == fairs[j][0]) j++;
int mx = -INF;
int cop = i;
for (int i = cop; i < j; i++) {
int day, money, pos;
day = fairs[i][0];
pos = fairs[i][1];
money = fairs[i][2];
// debug("new uwa");
// debug(lft.qry(pos), pos, d);
// debug(lft.qry(pos) - pos * d);
// debug(rt.qry(pos), pos, u);
// debug(pos * u + rt.qry(pos));
ndp[i] = max(lft.qry(pos) - pos * d, pos * u + rt.qry(pos)) + money;
dp[i] = max(ndp[i], mx + money - pos * d);
mx = max(mx + money, ndp[i] + pos * d);
}
mx = -INF;
for (int i = j - 1; i >= cop; i--) {
int day, money, pos;
day = fairs[i][0];
pos = fairs[i][1];
money = fairs[i][2];
dp[i] = max(dp[i], mx + money + pos * u);
mx = max(mx + money, ndp[i] - pos * u);
lft.upd(pos, -1, dp[i] + pos * d);
rt.upd(-1, pos, dp[i] - pos * u);
}
}
int ret = 0;
// debug(dp);
// debug(ndp);
for (int i = 0; i < n; i++) {
ret = max(ret, dp[i] - abs(fairs[i][1] - s) * (fairs[i][1] < s ? d : u));
}
cout << ret << '\n';
return 0;
}
Compilation message
salesman.cpp: In function 'int main()':
salesman.cpp:152:11: warning: variable 'day' set but not used [-Wunused-but-set-variable]
152 | int day, money, pos;
| ^~~
salesman.cpp:167:11: warning: variable 'day' set but not used [-Wunused-but-set-variable]
167 | int day, money, pos;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
31596 KB |
Output is correct |
2 |
Correct |
20 ms |
31596 KB |
Output is correct |
3 |
Correct |
21 ms |
31724 KB |
Output is correct |
4 |
Correct |
25 ms |
31852 KB |
Output is correct |
5 |
Correct |
31 ms |
32000 KB |
Output is correct |
6 |
Correct |
77 ms |
33260 KB |
Output is correct |
7 |
Correct |
176 ms |
35692 KB |
Output is correct |
8 |
Correct |
334 ms |
39916 KB |
Output is correct |
9 |
Correct |
499 ms |
43500 KB |
Output is correct |
10 |
Correct |
906 ms |
55276 KB |
Output is correct |
11 |
Correct |
1032 ms |
55992 KB |
Output is correct |
12 |
Correct |
1370 ms |
63016 KB |
Output is correct |
13 |
Correct |
1366 ms |
63272 KB |
Output is correct |
14 |
Correct |
1677 ms |
65536 KB |
Output is correct |
15 |
Correct |
1452 ms |
65536 KB |
Output is correct |
16 |
Correct |
1720 ms |
65536 KB |
Output is correct |
17 |
Correct |
21 ms |
31596 KB |
Output is correct |
18 |
Correct |
22 ms |
31724 KB |
Output is correct |
19 |
Correct |
25 ms |
31744 KB |
Output is correct |
20 |
Correct |
26 ms |
31864 KB |
Output is correct |
21 |
Correct |
26 ms |
31852 KB |
Output is correct |
22 |
Correct |
31 ms |
31980 KB |
Output is correct |
23 |
Correct |
32 ms |
31980 KB |
Output is correct |
24 |
Correct |
31 ms |
31980 KB |
Output is correct |
25 |
Correct |
356 ms |
39276 KB |
Output is correct |
26 |
Correct |
638 ms |
47084 KB |
Output is correct |
27 |
Correct |
1135 ms |
58104 KB |
Output is correct |
28 |
Correct |
1149 ms |
59128 KB |
Output is correct |
29 |
Correct |
1731 ms |
65536 KB |
Output is correct |
30 |
Correct |
1671 ms |
65536 KB |
Output is correct |