#include <bits/stdc++.h>
#include <chrono>
#define ll long long int
#define endl '\n'
#define vn vector<ll>
#define vi vector<pair <ll,ll>>
using namespace std;
using namespace std::chrono;
const int MAX_N = 1e9 + 7;
#define pii pair<ll,ll>
const ll INF = 0x3f3f3f3f3f3f3f3f;
#define pb push_back
#define srt(vp) sort(vp.begin(), vp.end())
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
auto start = high_resolution_clock::now();
ll n,p,q;
cin >> n >> p >> q;
vn v(n);
for (auto &x : v) cin >> x;
sort(v.begin(), v.end());
ll lo = 1;
ll ho = 1e9; // Modified to set upper bound correctly
while (lo < ho){ // Modified to ensure exit condition
ll mid = (lo + ho) / 2;
ll dp[p+1][q+1] = {}; // Added initialization
bool check = false;
for (ll i = 0; i <= p; i++){
for (ll j = 0; j <= q; j++){
ll x = i > 0 ? dp[i-1][j] : -1; // Initialize x and conditionally set
ll y = j > 0 ? dp[i][j-1] : -1; // Initialize y and conditionally set
if (x < n-1 && i > 0){ // Fixed boundary check
ll newdist = upper_bound(v.begin(), v.end(), v[x] + mid) - v.begin() - 1;
dp[i][j] = max(dp[i][j], newdist);
}
if (y < n-1 && j > 0){ // Fixed boundary check
ll newdist1 = upper_bound(v.begin(), v.end(), v[y] + 2 * mid) - v.begin() - 1; // Doubled the range for large camera
dp[i][j] = max(dp[i][j], newdist1);
}
if (dp[i][j] >= n-1){ // Fixed comparison to the last index
check = true;
}
}
}
if (check) ho = mid; // Fixed the binary search update condition
else lo = mid + 1; // Ensure progress in the loop
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Time taken by function: " << duration.count() << " microseconds" << endl; // Un-commented for utility
//cout << lo << endl; // This line outputs the result
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |