Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Teleport time depends only on heights, not tower indices. Once you know the starting height, the actual positions are basically decoration.
A move to a lower height never helps you reach a maximum-height tower. It spends time and leaves you with a worse tower, which is impressively bad business.
Let the starting height be . If you only move upward from height to height , the elapsed time is exactly because the height differences telescope.
So when you are at height , the next upward move to height is legal exactly when . The allowed gap is the original starting height, and it does not grow.
Sort the tower heights. Starting from , greedily walk through increasing heights; if the next distinct height is more than above your current one, you are stuck forever. Otherwise you can reach the maximum.
The trick is to stop thinking about tower indices. They barely matter. Teleport duration is based only on height difference, so towers with the same height are interchangeable.
Let
be the height of the tower where you start.
Suppose you are on a tower of height at time , and you want to teleport upward to height .
The teleport takes seconds. During that whole time, you are still on height . From the examples, leaving exactly when the old tower gets covered is allowed, so the move is legal iff
Now assume we have only moved upward so far. If we started at height and are currently at height , then the total time spent is
because all the upward height differences telescope:
Plug into the teleport condition:
Simplify:
That is the whole problem. Your maximum allowed upward height gap is your starting height .
Could dropping to a lower tower ever help? No.
Moving downward costs time too, but it does not get you closer to the maximum height. Even worse, it puts you on a lower tower while the water keeps rising. So any path with downward moves can only be worse than a path that skips that nonsense. We can assume the useful path is nondecreasing in height.
Sort all heights.
Start with cur = h_k. Look at the available tower heights in increasing order. If the next height satisfies
then you can reach it, so set cur = x.
If instead
then you cannot reach , and you also cannot reach anything taller than , because those are even farther away. You are stuck. No secret anime teleport arc is coming.
So the algorithm is:
NO.YES.Lemma 1: There is always an optimal successful path that never moves to a lower height.
A downward move spends positive time unless the height is equal, and it ends at a height no larger than before. Since the goal is to reach a maximum-height tower and water only rises, such a move cannot improve future possibilities. Removing downward movement never makes the situation worse.
Lemma 2: If we have reached height using only upward moves from starting height , then the elapsed time is .
Each upward teleport contributes exactly the increase in height. Summing all increases from to telescopes to .
Lemma 3: From a reached height , an upward move to height is possible iff .
By Lemma 2, the current time is . The move takes seconds and must finish before or exactly when height becomes unusable:
This is equivalent to .
Lemma 4: If the sorted scan finds a gap larger than , the maximum height is unreachable.
Let cur be the largest reachable height so far, and let be the next available height above it. If , then by Lemma 3 we cannot move from cur to . Every later height is at least , so it is also unreachable from cur. There is no intermediate tower to bridge the gap.
Lemma 5: If every scanned gap is at most , the maximum height is reachable.
The scan constructs a valid sequence of upward moves. Each move has height difference at most , so by Lemma 3 every move is legal. Eventually the sequence reaches the maximum height.
Therefore, the greedy sorted scan answers YES exactly when reaching a maximum-height tower is possible.
Sorting dominates:
per test case, with total , so this easily fits.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<ll> h(n);
for (ll &x : h) cin >> x;
ll start = h[k - 1];
sort(h.begin(), h.end());
ll cur = start;
bool ok = true;
for (ll x : h) {
if (x < cur) continue;
if (x - cur > start) {
ok = false;
break;
}
cur = x;
}
cout << (ok ? "YES" : "NO") << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<ll> h(n);
for (ll &x : h) cin >> x;
ll start = h[k - 1];
sort(h.begin(), h.end());
ll cur = start;
bool ok = true;
for (ll x : h) {
if (x < cur) continue;
if (x - cur > start) {
ok = false;
break;
}
cur = x;
}
cout << (ok ? "YES" : "NO") << '\n';
}
return 0;
}