QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#476719#9127. Optimal Train Operationucup-team1447#WA 0ms9628kbC++143.2kb2024-07-13 20:51:212024-07-13 20:51:23

Judging History

你现在查看的是最新测评结果

  • [2024-07-13 20:51:23]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:9628kb
  • [2024-07-13 20:51:21]
  • 提交

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 add1(std::deque<std::pair<long long, long long>> &d, std::pair<long long, long long> e)
{
    if (!d.empty() && e.first == d.back().first && e.second >= d.back().second)
        return;
    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);
}
void add2(std::deque<std::pair<long long, long long>> &d, std::pair<long long, long long> e)
{
    if (!d.empty() && e.first == d.back().first && e.second >= d.back().second)
        return;
    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])
            add1(d, std::make_pair(-j, dp[j]));
            --j;
        }
        if (d.empty())
            continue;
        int l = 0, r = d.size() - 1;
        while (l < r)
        {
            int mid = (l + r) >> 1;
            if (get(d[mid], v[i]) <= get(d[mid + 1], v[i]))
                r = mid;
            else
                l = mid + 1;
        }
        dp[i] = std::min(dp[i], get(d[l], v[i]) + 1ll * v[i] * i + a[i]);
    }
    d.clear();
    for (int i = r, j = l; i > mid; --i)
    {
        while (j <= mid && v[j] > v[i])
        {
            // dp[i]=min(dp[j]+max(v[i],v[j])*(i-j)+a[i])
            add2(d, std::make_pair(v[j], dp[j] - v[j] * j));
            ++j;
        }
        if (d.empty())
            continue;
        int l = 0, r = d.size() - 1;
        while (l < r)
        {
            int mid = (l + r) >> 1;
            if (get(d[mid], i) <= get(d[mid + 1], i))
                r = mid;
            else
                l = mid + 1;
        }
        dp[i] = std::min(dp[i], get(d[l], i) + a[i]);
    }
    d.clear();
    // for (int i = l; i <= r; ++i)
    //     std::cout << v[i] << " \n"[i == r];
    for (int i = l; i <= r; ++i)
        std::cout << dp[i] << " \n"[i == r];
    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: 0
Wrong Answer
time: 0ms
memory: 9628kb

input:

4
3 1 4 1
5 9 2

output:

0 8
0 8 15
0 8 15 14 16
14 15
15

result:

wrong answer 1st words differ - expected: '15', found: '0'