QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#379445#8572. Passing Gameucup-team1055#WA 78ms9240kbC++203.0kb2024-04-06 17:32:162024-04-06 17:32:16

Judging History

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

  • [2024-04-06 17:32:16]
  • 评测
  • 测评结果:WA
  • 用时:78ms
  • 内存:9240kb
  • [2024-04-06 17:32:16]
  • 提交

answer

#include <bits/stdc++.h>

#define rep(i,s,n) for(int i = int(s); i < int(n); i++)
#define rrep(i,s,n) for(int i = int(n) - 1; i >= int(s); i--)
#define all(v) (v).begin(), (v).end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

bool chmin(auto &a, auto b) {
    if(a <= b) return false;
    a = b;
    return true;
}

bool chmax(auto &a, auto b) {
    if(a >= b) return false;
    a = b;
    return true;
}

void solve() {
    int n, k;
    std::cin >> n >> k;
    std::vector<ll> x(n), s(n);
    rep(i,0,n) std::cin >> x[i];
    rep(i,0,n) std::cin >> s[i];
    s[n-1] = 0;
    std::vector<int> ids(n);
    std::iota(all(ids), 0);
    std::sort(all(ids), [&](int i, int j) -> bool {
        return x[i] < x[j];
    });
    int id0 = -1;
    rep(i,0,n) {
        if(ids[i] == 0) {
            id0 = i;
            break;
        }
    }
    assert(id0 != -1);
    std::vector<int> left, right;
    left.emplace_back(0);
    right.emplace_back(0);
    rrep(i,0,id0) {
        if(s[left.back()] > s[i]) {
            left.emplace_back(i);
        }
    }
    rep(i,id0+1,n) {
        if(s[right.back()] > s[i]) {
            right.emplace_back(i);
        }
    }
    ll ans = std::abs(x[0] - x[n-1]) * s[0];
    int sl = left.size(), sr = right.size();
    std::vector<int> nl(sl), nr(sr);
    rep(i,0,sl) {
        nl[i] = std::upper_bound(all(right), left[i], [&](int a, int b) -> bool {
            return s[a] > s[b];
        }) - right.begin();
    }
    rep(i,0,sr) {
        nr[i] = std::upper_bound(all(left), right[i], [&](int a, int b) -> bool {
            return s[a] > s[b];
        }) - left.begin();
    }
    const ll INF = std::numeric_limits<ll>::max() / 4;
    // assert(200'000'000'000'000'000 < INF);
    std::vector<ll> dpl(sl, INF), dpr(sr, INF);
    dpl[0] = 0;
    dpr[0] = 0;
    // chmin(k, 120);
    while(k--) {
        auto nxtl = dpl;
        auto nxtr = dpr;
        rep(i,0,sl) {
            if(i < sl - 1) {
                chmin(dpl[i+1], dpl[i] + s[left[i]] * std::abs(x[left[i]] - x[left[i+1]]));
            }
            if(nl[i] < sr) {
                chmin(nxtr[nl[i]], dpl[i] + s[left[i]] * std::abs(x[left[i]] - x[right[nl[i]]]));
            }
        }
        rep(i,0,sr) {
            if(i < sr - 1) {
                chmin(dpr[i+1], dpr[i] + s[right[i]] * std::abs(x[right[i]] - x[right[i+1]]));
            }
            if(nr[i] < sl) {
                chmin(nxtl[nr[i]], dpr[i] + s[right[i]] * std::abs(x[right[i]] - x[left[nr[i]]]));
            }
        }
        if(left.back() == n-1) {
            chmin(ans, dpl.back());
        }
        else if(right.back() == n-1) {
            chmin(ans, dpr.back());
        }
        else assert(0);
        std::swap(dpl, nxtl);
        std::swap(dpr, nxtr);
    }
    std::cout << ans << '\n';
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t;
    std::cin >> t;
    while(t--) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3548kb

input:

2
4 2
3 2 1 6
3 1 1 3
2 0
1 2
1 2

output:

7
1

result:

ok 2 number(s): "7 1"

Test #2:

score: -100
Wrong Answer
time: 78ms
memory: 9240kb

input:

1
300000 204334
809492393 304618667 173130445 377106790 364888630 949045125 622060683 557772818 216607577 848817467 862855568 507840723 120816645 639713488 741781998 682531787 685261161 601686403 355792373 162819930 710057718 234560726 998604853 678957602 485413982 855985802 109303681 979706626 4822...

output:

464895143881666786

result:

wrong answer 1st numbers differ - expected: '31313390701066', found: '464895143881666786'