DMOJ · bts17p4

Wet Mud

This C++ solution uses simulation for DMOJ bts17p4 Wet Mud. Read the reasoning, inspect the code, or try your own test case below.

bts17p4Implementation & simulationSimulationC++39 lines
Solution237of 248
Open official problem ↗ Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Simulation

Wet Mud matches positions, drying times, jump limit, and the minimum feasible crossing time.

Implementation & simulation

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

Open official problem ↗View exact source file ↗
Implementation

wet_mud.cpp

C++

    #include <bits/stdc++.h>
     
    using namespace std;
     
    const int INF = 1e9 + 7;
     
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        int n, m, j;
        cin >> n >> m >> j;
        vector<int> dry(n + 2, INF);
        dry[0] = dry[n + 1] = 0; 
        for (int i = 0; i < m; i++){
            int p, t; cin >> p >> t;
            dry[p] = t;
        }
        vector<int> v(n + 2, INF);
        v[0] = 0;
        deque<int> dq;
        dq.push_back(0);
        for (int i = 1; i <= n + 1; i++){
            while (!dq.empty() && dq.front() < i - j) dq.pop_front();
            if (dry[i] == INF || dq.empty()){
                v[i] = INF;
            }else {
                int best = v[dq.front()];
                if (best == INF) v[i] = INF;
                else v[i] = max(best, dry[i]);
            }
            if (v[i] != INF) {
                while (!dq.empty() && v[dq.back()] >= v[i]) dq.pop_back();
                dq.push_back(i);
            }
        }
            if (v[n + 1] == INF) cout << -1 << '\n';
            else cout << v[n + 1] << '\n';
            return 0;
    }
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.