Submission #728067

#TimeUsernameProblemLanguageResultExecution timeMemory
728067grossly_overconfidentRainforest Jumps (APIO21_jumps)C++17
37 / 100
4045 ms27072 KiB
//#include "jumps.h" #include <bits/stdc++.h> using namespace std; #define endl '\n' //#define int long long vector<vector<int>> adj; bool verdict = true; void init(int n, vector<int> h){ for (int i = 0; i < n; ++i){ if (i + 1 != h[i]){ verdict = false; break; } } if (verdict){ return; } adj.resize(n); set<pair<int, int>> s; vector<int> m(n); for (int i = 0; i < n; ++i){ s.insert({i, h[i]}); m[h[i] - 1] = i; } for (int i = 0; i < n; ++i){ vector<int> to_push; auto it = s.find({m[i], i + 1}); if (it != s.begin()){ auto it2 = it; it2--; to_push.push_back((*it2).first); } if (it != (--s.end())){ auto it2 = it; it2++; to_push.push_back((*it2).first); } adj[m[i]] = to_push; s.erase(it); } } int minimum_jumps(int a, int b, int c, int d){ if (verdict){ if (b >= c && a <= d){ return 0; } if (a < c){ return c - b; } return -1; } queue<pair<int, int>> q; unordered_set<int> s, f; vector<bool> v(adj.size()); for (int i = a; i <= b; ++i){ s.insert(i); } for (int i = c; i <= d; ++i){ f.insert(i); } for (int i : s){ q.push({0, i}); } while (!q.empty()){ auto t = q.front(); q.pop(); if (v[t.second]){ continue; } v[t.second] = true; if (f.find(t.second) != f.end()){ return t.first; } for (int i : adj[t.second]){ q.push({t.first + 1, i}); } } return -1; } /* signed main(){ //cin.tie(0); //iostream::sync_with_stdio(0); int n, q; cin >> n >> q; vector<int> k(n); for (int i = 0; i < n; ++i){ cin >> k[i]; } init(n, k); for (int i = 0; i < adj.size(); ++i){ cout << i << ": "; for (auto j : adj[i]){ cout << j << " "; } cout << endl; } for (int i = 0; i < q; ++i){ int a, b, c, d; cin >> a >> b >> c >> d; cout << minimum_jumps(a, b, c, d) << endl; } return 0; } */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...