/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 3ms 364.0 KiB
#3 Accepted 98ms 344.0 KiB
#4 Time Exceeded ≥2098ms ≥804.0 KiB
#5 Time Exceeded ≥2098ms ≥940.0 KiB
#6 Time Exceeded ≥2099ms ≥4.879 MiB
#7 Time Exceeded ≥2100ms ≥18.211 MiB
#8 Time Exceeded ≥2100ms ≥10.121 MiB
#9 Time Exceeded ≥2103ms ≥9.984 MiB
#10 Time Exceeded ≥2099ms ≥4.156 MiB
#11 Time Exceeded ≥2098ms ≥4.164 MiB
#12 Time Exceeded ≥2101ms ≥18.145 MiB
#13 Time Exceeded ≥2099ms ≥18.102 MiB
#14 Time Exceeded ≥2099ms ≥35.578 MiB
#15 Time Exceeded ≥2097ms ≥4.879 MiB
#16 Time Exceeded ≥2000ms ≥1.305 MiB
#17 Time Exceeded ≥2101ms ≥1.297 MiB
#18 Time Exceeded ≥2001ms ≥4.898 MiB
#19 Time Exceeded ≥2101ms ≥2.883 MiB
#20 Time Exceeded ≥2001ms ≥2.883 MiB

Code

#include <bits/stdc++.h>
using namespace std;
using ll=long long;

ll gcd_(ll a, ll b) {
    return b==0? a : gcd_(b, a%b);
}
void solve() {
    ll n, k;
    cin >> n >> k;
    vector<ll> arr(n);

    for (ll i = 0; i < n; i++) {
        cin >> arr[i];
    }

    vector<ll> dp(n + 1, LLONG_MAX);
    dp[1] = 0;

    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
    pq.push({0, 1}); // (cost, index)

    while (!pq.empty()) {
    pair<ll, ll> topElement = pq.top();
    ll currentCost = topElement.first;
    ll i = topElement.second;
    pq.pop();

    if (currentCost > dp[i]) {
        continue;
    }

    //Move to the next position (i + 1)
    if (i + 1 <= n) {
        ll nextCost = currentCost + k;
        if (nextCost < dp[i + 1]) {
            dp[i + 1] = nextCost;
            pq.push({nextCost, i + 1});
        }
    }

    // Jump to any position j > i
    for (ll j = i + 1; j <= n; ++j) {
        ll jumpCost = arr[i - 1] / gcd_(arr[i - 1], arr[j - 1]);
        ll totalCost = currentCost + jumpCost;

        if (totalCost < dp[j]) {
            dp[j] = totalCost;
            pq.push({totalCost, j});
        }
    }
    }

    cout << dp[n] << endl;
}


int main(){
    int t;
    cin>>t;
    while(t--){
        solve();
    }
return 0;
}

Information

Submit By
Type
Submission
Problem
P1099 Which way to go
Contest
Brain Booster #6
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 17:49:05
Judged At
2024-10-03 17:49:05
Judged By
Score
15
Total Time
≥2103ms
Peak Memory
≥35.578 MiB