# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
556981 | timreizin | Salesman (IOI09_salesman) | C++17 | 3086 ms | 65536 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <string>
#include <numeric>
#include <cmath>
#include <cassert>
#include <array>
#include <numeric>
using namespace std;
using ll = long long;
class SegmentTree
{
private:
int amount;
vector<ll> tree;
ll get(int a, int b, int l, int r, int v)
{
if (a > r || b < l) return -1e9;
if (a == l && b == r) return tree[v];
int m = (l + r) >> 1;
return max(get(a, min(b, m), l, m, v << 1), get(max(a, m + 1), b, m + 1, r, (v << 1) + 1));
}
void build(int l, int r, int v)
{
if (l > r) return;
if (l == r)
{
tree[v] = -1e9;
return;
}
int m = (l + r) >> 1;
build(l, m, v << 1);
build(m + 1, r, (v << 1) + 1);
tree[v] = max(tree[v << 1], tree[(v << 1) + 1]);
}
void update(int p, int l, int r, int v, ll value)
{
if (l > r) return;
if (l == r)
{
tree[v] = value;
return;
}
int m = (l + r) >> 1;
if (p <= m) update(p, l, m, v << 1, value);
else update(p, m + 1, r, (v << 1) + 1, value);
tree[v] = max(tree[v << 1], tree[(v << 1) + 1]);
}
public:
SegmentTree(int n = 0) : amount(n)
{
tree.resize(4 * amount + 5);
build(0, n - 1, 1);
}
ll get(int a, int b)
{
return get(a, b, 0, amount - 1, 1);
}
void update(int p, ll value)
{
update(p, 0, amount - 1, 1, value);
}
};
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n;
ll d, u, s;
cin >> n >> u >> d >> s;
vector<tuple<int, ll, ll>> fairs(n);
for (auto &[t, l, m] : fairs) cin >> t >> l >> m;
sort(fairs.begin(), fairs.end());
vector<vector<pair<ll, ll>>> times;
int last = get<0>(fairs.front());
times.push_back(vector<pair<ll, ll>>());
for (auto [t, l, m] : fairs)
{
if (t != last) times.push_back(vector<pair<ll, ll>>());
times.back().emplace_back(l, m);
last = t;
}
ll res = 0;
SegmentTree left(500005), right(500005);
left.update(s, s * d);
right.update(s, -s * u);
for (auto fairs : times)
{
vector<ll> dp(fairs.size(), -1e9);
for (int i = 0; i < dp.size(); ++i)
{
ll sum = 0;
for (int j = i; j >= 0; --j)
{
sum += fairs[j].second;
ll bestj = max(left.get(0, fairs[j].first) - d * fairs[j].first, right.get(fairs[j].first, 500001) + u * fairs[j].first);
dp[i] = max(dp[i], bestj + sum - (fairs[i].first - fairs[j].first) * d);
}
}
for (int i = (int)dp.size() - 1; i >= 0; --i)
{
ll sum = 0;
for (int j = i; j < dp.size(); ++j)
{
sum += fairs[j].second;
ll bestj = max(left.get(0, fairs[j].first) - d * fairs[j].first, right.get(fairs[j].first, 500001) + u * fairs[j].first);
dp[i] = max(dp[i], bestj + sum - (fairs[j].first - fairs[i].first) * u);
}
}
for (int i = 0; i < dp.size(); ++i)
{
ll move = (s < fairs[i].first ? u * (fairs[i].first - s) : d * (s - fairs[i].first));
res = max(res, dp[i] - move);
left.update(fairs[i].first, dp[i] + fairs[i].first * d);
right.update(fairs[i].first, dp[i] - fairs[i].first * u);
}
}
cout << res;
return 0;
}
/*
4 5 3 100
2 80 100
20 125 130
10 75 150
5 120 110
*/
/*
4 5 3 100
1 80 100
1 125 130
1 75 150
1 120 110
*/
//125 + 90 -
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |