QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#379522#8572. Passing Gameucup-team1055#WA 75ms9044kbC++203.0kb2024-04-06 17:45:262024-04-06 17:45:26

Judging History

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

  • [2024-04-06 17:45:26]
  • 评测
  • 测评结果:WA
  • 用时:75ms
  • 内存:9044kb
  • [2024-04-06 17:45:26]
  • 提交

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[ids[i]]) {
            left.emplace_back(ids[i]);
        }
    }
    rep(i,id0+1,n) {
        if(s[right.back()] > s[ids[i]]) {
            right.emplace_back(ids[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;
    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: 1ms
memory: 3820kb

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: 0
Accepted
time: 75ms
memory: 9012kb

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:

31313390701066

result:

ok 1 number(s): "31313390701066"

Test #3:

score: 0
Accepted
time: 75ms
memory: 5520kb

input:

3
100000 65460
217141764 710454586 789075415 24849107 685675008 839804815 638763480 327755609 43827967 390187172 301370841 622696676 598237196 232099091 211987715 416876077 572665966 73382836 520033984 808399404 752832432 341795744 434460344 535426588 136624537 997406768 297342165 558882675 26863877...

output:

70635841128944
47230361360721
59110547802683

result:

ok 3 number(s): "70635841128944 47230361360721 59110547802683"

Test #4:

score: 0
Accepted
time: 70ms
memory: 9044kb

input:

1
300000 101975
207258305 525434317 528778163 645316642 562113679 143398489 9114413 669854123 106324041 841914487 21419012 308025536 689200225 263298218 39377353 860366080 24610184 43404209 529054797 902238799 422737070 484129934 967667618 953541323 338625285 115085955 363490839 998893783 877857789 ...

output:

40311829457542

result:

ok 1 number(s): "40311829457542"

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 3820kb

input:

18
17 0
500000000 499999997 500000010 499999965 500000118 499999609 500001291 499995739 500014064 499953589 500153157 499494579 501667889 494495965 518163316 440061055 697798520
197798520 59938945 18163316 5504035 1667889 505421 153157 46411 14064 4261 1291 391 118 35 10 3 1
17 1
500000000 499999997...

output:

39124254514190400
15506866876
14901483521
14901483521
14596599454
14596599454
14327495899
14327495899
14069829018
14069829018
13814295534
13814295534
13559037370
13559037370
13303815695
13303815695
13061698660
13061698660

result:

wrong answer 1st numbers differ - expected: '15506866876', found: '39124254514190400'