Codeforces · 2185E

The Robotic Rush

This C++ solution uses binary search for Codeforces 2185E The Robotic Rush. Read the reasoning, inspect the code, or try your own test case below.

2185EData structuresBinary searchC++70 lines
Solution220of 248
Open official problem ↗ Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Binary search

The Robotic Rush matches robot commands, spike coordinates, and first-failure displacement counting.

Data structures

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

E_The_Robotic_Rush.cpp

C++

     
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int inf = 1e9;
    const long long INF = 1e17; //❄️
    #define int long long
    void solve() {
        int n, m, k; cin >> n >> m >> k;
        //we want to precompute how much each update will kill
        vector<int> a(n), b(m);
        for (int i = 0; i < n; i++) cin >> a[i];
        for (int i = 0; i < m; i++) cin >> b[i];
        sort(b.begin(), b.end());
        unordered_map<int, int> kill, first;
        unordered_set<int> used;
        string s; cin >> s;
        int cur = 0;
        for (int i = 0; i < s.size(); i++) {
        if (s[i] == 'L') cur--;
        else cur++;
        if (!first.count(cur)) {
            first[cur] = i + 1;
        }
    }
    for (int i = 0; i < n; i++) {
        int x = a[i];
        int indx = lower_bound(b.begin(), b.end(), x) - b.begin();
        int best_time = INF, dif = 0;
        if (indx < m) {
            int d = b[indx] - x;
            if (first.count(d)) {
                if (first[d] < best_time) {
                    best_time= first[d];
                    dif = d;
                }
            }
        }
        if (indx > 0) {
            int d = b[indx - 1] - x;
            if (first.count(d)) {
                if (first[d] < best_time) {
                    best_time = first[d];
                    dif = d;
                }
            }
        }
        if (best_time != INF) {
            kill[dif]++;
        }
    }
        int pos = 0, dead = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == 'L') pos--;
            else pos++;
            if (!used.count(pos)) {
                if (kill.count(pos)) dead += kill[pos];
                used.insert(pos);
            }
            cout << n - dead << " \n"[i == s.size() - 1];
        }
    }
    signed main() {
        ios::sync_with_stdio(0); cin.tie(0); 
        int t; cin >> t;
        while (t--) {
            solve();
        }
        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.