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.
For any fixed stop , can you write the student's total travel time as two independent pieces: time on the bus and time running?
The bus starts at and moves only along the -axis with constant speed . So reaching stop at always takes exactly .
If he gets off at stop , he then runs straight to the university at . The running distance is
so the running time is .
Now thereβs no hidden DP, no binary search, no black magic. For every stop from to , compute
and choose the stop with minimum .
The only gotcha is ties. If two stops have the same total time (compare with some tiny ), pick the one closer to the university, i.e. the one with smaller . Since , a plain scan is more than enough. Nice and brutally simple.
This is one of those problems where the hardest part is resisting the urge to invent something fancier than necessary. Don't. The problem is basically one formula per stop.
If the student gets off at stop (and note that is forbidden), then his total arrival time is
Why?
That's the whole model. No transitions, no state, no drama.
We only have stops. That is tiny. So just try every valid stop:
For each stop, compute and keep the best one.
The statement says: if several stops give the same earliest arrival time, choose the stop closest to the university.
That means among equal-time candidates, minimize
So the comparison rule is:
Because we are using floating-point arithmetic, compare times with a small epsilon, e.g.
Using long double is perfectly safe here.
For every allowed stop , the student's route is uniquely determined:
The formula for computes exactly the total time of that route. Since the student must get off at some stop from to , the optimal answer is simply the stop with minimum . If several stops share that minimum, the statement explicitly tells us to choose the one with minimum distance to the university. Scanning all candidates and applying these rules therefore returns the correct stop.
No tricks. Just geometry plus implementation that doesn't screw up floating-point ties.
We evaluate one formula per stop.
With , this is hilariously cheap.
#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 n;
long double vb, vs;
cin >> n >> vb >> vs;
vector<ll> x(n + 1);
for (int i = 1; i <= n; ++i) cin >> x[i];
ll xu, yu;
cin >> xu >> yu;
const long double EPS = 1e-12L;
int ans = 2;
long double bestTime = 1e100L;
long double bestDist = 1e100L;
for (int i = 2; i <= n; ++i) {
long double dx = (long double)x[i] - (long double)xu;
long double dy = (long double)yu;
long double dist = sqrtl(dx * dx + dy * dy);
long double total = (long double)x[i] / vb + dist / vs;
if (total + EPS < bestTime ||
(fabsl(total - bestTime) <= EPS && dist + EPS < bestDist)) {
bestTime = total;
bestDist = dist;
ans = i;
}
}
cout << ans << '\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 n;
long double vb, vs;
cin >> n >> vb >> vs;
vector<ll> x(n + 1);
for (int i = 1; i <= n; ++i) cin >> x[i];
ll xu, yu;
cin >> xu >> yu;
const long double EPS = 1e-12L;
int ans = 2;
long double bestTime = 1e100L;
long double bestDist = 1e100L;
for (int i = 2; i <= n; ++i) {
long double dx = (long double)x[i] - (long double)xu;
long double dy = (long double)yu;
long double dist = sqrtl(dx * dx + dy * dy);
long double total = (long double)x[i] / vb + dist / vs;
if (total + EPS < bestTime ||
(fabsl(total - bestTime) <= EPS && dist + EPS < bestDist)) {
bestTime = total;
bestDist = dist;
ans = i;
}
}
cout << ans << '\n';
return 0;
}