QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#476606 | #9127. Optimal Train Operation | ucup-team1447# | WA | 1ms | 9964kb | C++14 | 2.1kb | 2024-07-13 20:25:29 | 2024-07-13 20:25:29 |
Judging History
answer
// This Code was made by Chinese_zjc_.
#include <bits/stdc++.h>
int n, a[500005], c[500005], v[500005];
long long dp[500005];
void add(std::deque<std::pair<long long, long long>> &d, std::pair<long long, long long> e)
{
while (d.size() >= 2 && 1ll * (e.second - d.end()[-1].second) * (e.first - d.end()[-2].first) <=
1ll * (e.second - d.end()[-2].second) * (e.first - d.end()[-1].first))
d.pop_back();
d.push_back(e);
}
long long get(std::pair<long long, long long> l, long long x) { return l.first * x + l.second; }
void solve(int l, int r)
{
if (l == r)
return;
int mid = (l + r) >> 1;
solve(l, mid);
v[mid] = 0;
for (int i = mid; --i >= l;)
v[i] = std::max(v[i + 1], c[i]);
for (int i = mid + 1; i <= r; ++i)
v[i] = std::max(v[i - 1], c[i - 1]);
std::deque<std::pair<long long, long long>> d;
for (int i = mid + 1, j = mid; i <= r; ++i)
{
while (j >= l && v[j] <= v[i])
{
// dp[i]=min(dp[j]+max(v[i],v[j])*(i-j)+a[i])
add(d, std::make_pair(-j, dp[j]));
--j;
}
while (d.size() >= 2 && get(d[0], v[i]) >= get(d[1], v[i]))
d.pop_front();
if (!d.empty())
dp[i] = std::min(dp[i], get(d[0], v[i]) + 1ll * v[i] * i + a[i]);
}
d.clear();
for (int i = r, j = l; i >= l; --i)
{
while (j <= mid && v[j] > v[i])
{
// dp[i]=min(dp[j]+max(v[i],v[j])*(i-j)+a[i])
add(d, std::make_pair(v[j], dp[j] - v[j] * j));
++j;
}
while (d.size() >= 2 && get(d[0], v[i]) >= get(d[1], v[i]))
d.pop_front();
if (!d.empty())
dp[i] = std::min(dp[i], get(d[0], i) + a[i]);
}
d.clear();
solve(mid + 1, r);
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin >> n;
std::fill(dp + 1, dp + 1 + n, LLONG_MAX / 2);
for (int i = 0; i != n; ++i)
std::cin >> c[i];
for (int i = 1; i != n; ++i)
std::cin >> a[i];
solve(0, n);
std::cout << dp[n] << std::endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 9752kb
input:
4 3 1 4 1 5 9 2
output:
15
result:
ok "15"
Test #2:
score: 0
Accepted
time: 1ms
memory: 9664kb
input:
9 28 35 19 27 84 98 78 79 60 40 35 54 63 72 71 27 94
output:
682
result:
ok "682"
Test #3:
score: 0
Accepted
time: 1ms
memory: 9964kb
input:
7 476190629 262703497 211047202 971407775 628894325 731963982 822804784 877914575 602436426 24979445 861648772 623690081 433933447
output:
5339182432
result:
ok "5339182432"
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 9692kb
input:
8 910286918 802329211 404539679 303238506 317063340 492686568 773361868 125660016 430302156 982631932 161735902 880895728 923078537 707723857 189330739
output:
6381119582
result:
wrong answer 1st words differ - expected: '6166732840', found: '6381119582'