This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1ll << 60ll;
int max_score(int N, int X, int Y, ll K,
vector<int> U, vector<int> V, vector<int> W)
{
// Subtask 4:
// Find optimal distance X should move to the right
// O(N²)
int best = 0;
for (int r = X; r < N; r++)
{
vector<ll> closingTime(N);
int res = 0;
ll k_sum = 0;
for (int i = X + 1; i <= r; i++)
{
k_sum += closingTime[i-1] + W[i];
closingTime[i] = closingTime[i-1] + W[i];
res++;
}
if (k_sum > K)
break;
priority_queue<pair<ll, pair<int, int>>> q;
q.push({0, {X, 0}});
q.push({0, {Y, 1}});
vector<ll> dist(N), distY(N, INF);
distY[Y] = 0;
vector<int> visited(N);
while (!q.empty())
{
int t = q.top().second.second;
int s = q.top().second.first;
q.pop();
if (t == 0)
{
// X - can only go left
if (k_sum + dist[s] > K)
break;
k_sum += dist[s];
res++;
if (s)
{
dist[s-1] = dist[s] + W[s-1];
q.push({-dist[s-1], {s-1, 0}});
}
}
else
{
// Y - can go either way
ll addedDist = max(0ll, distY[s] - dist[s]);
if (k_sum + addedDist > K)
break;
k_sum += addedDist;
res++;
visited[s] = 1;
if (s && !visited[s-1])
{
// Go left
distY[s-1] = distY[s] + W[s-1];
q.push({-max(0ll, distY[s-1] - dist[s-1]), {s-1, 1}});
}
if (s < N - 1 && !visited[s+1])
{
// Go right
distY[s+1] = distY[s] + W[s];
q.push({-max(0ll, distY[s+1] - dist[s+1]), {s+1, 1}});
}
}
}
best = max(best, res);
}
return best;
/*
priority_queue<pair<ll, int>> q;
q.push({0, X});
q.push({0, Y});
vector<int> visited(N);
dist[Y] = 0;
int res = 0;
while (!q.empty())
{
int s = q.top().second;
q.pop();
if (visited[s])
continue;
visited[s] = 1;
K -= dist[s];
if (K < 0)
break;
res++;
for (auto node : adj[s])
{
if (!visited[node.first] && dist[s] + node.second < dist[node.first])
{
dist[node.first] = dist[s] + node.second;
q.push({-dist[node.first], node.first});
}
}
}
return res;*/
}
# | 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... |
# | 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... |