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 "shortcut.h"
#include <bits/stdc++.h>
#define N 200005
using namespace std;
vector<pair<int,int>> adj[N];
bool on_circle[N];
long long ret = 0;
long long dfs(int v,int par){
long long maxi = 0;
for(auto u:adj[v]){
if(u.first == par || on_circle[u.first])continue;
long long tmp = dfs(u.first,v) + u.second;
ret = max(ret,maxi + tmp);
maxi = max(maxi,tmp);
}
return maxi;
}
long long get_ans(vector<int> circle,vector<int> circle_edges){
assert(circle.size() == circle_edges.size());
for(auto u:circle){
on_circle[u] = 1;
}
ret = 0;
vector<long long> depths;
for(auto u:circle){
depths.push_back(dfs(u,-1));
}
long long circle_length = 0;
for(auto u:circle_edges){
circle_length += u;
}
vector<long long> d = {0};
for(int i = 1;i<circle.size();i++){
d.push_back(d[i-1] + circle_edges[i-1]);
}
deque<pair<int,long long>> s1;
int sz = circle.size();
vector<long long> sufval(sz+1,-1e18);
for(int i = sz-1;i>=0;i--){
sufval[i] = max(sufval[i+1],depths[i] - d[i] + circle_length);
}
int pt1 = -1;
for(int i = 0;i<circle.size();i++){
while(pt1 + 1 < circle.size() && (d[pt1+1] - d[i]) < circle_length - (d[pt1 + 1] - d[i])){
pt1++;
while(s1.size() && s1.back().second <= d[pt1] + depths[pt1]){
s1.pop_back();
}
s1.push_back({pt1,d[pt1] + depths[pt1]});
}
if(s1.size() && s1.front().first == i)
s1.pop_front();
if(s1.size()){
ret = max(ret,s1.front().second+depths[i] -d[i]);
}
ret = max(ret,sufval[pt1+1]+depths[i] +d[i]);
}
for(auto u:circle){
on_circle[u] = 0;
}
return ret;
}
long long find_shortcut(int n, std::vector<int> l, std::vector<int> d, int nwedge)
{
for(int i = 0;i<n-1;i++){
adj[i].push_back({i+1,l[i]});
adj[i+1].push_back({i,l[i]});
}
for(int i = 0;i<n;i++){
adj[i].push_back({i+n,d[i]});
adj[i+n].push_back({i,d[i]});
}
long long ans = 1e18;
for(int i = 0;i<n;i++){
vector<int> circle;
vector<int> circle_edges;
for(int j = i;j<n;j++){
{
circle.clear();
circle_edges.clear();
for(int c = i;c<=j;c++){
circle.push_back(c);
if(c != i)
circle_edges.push_back(l[c-1]);
}
circle_edges.push_back(nwedge);
adj[i].push_back({j,nwedge});
adj[j].push_back({i,nwedge});
ans = min(ans,get_ans(circle,circle_edges));
adj[i].pop_back();
adj[j].pop_back();
}
}
}
return ans;
}
Compilation message (stderr)
shortcut.cpp: In function 'long long int get_ans(std::vector<int>, std::vector<int>)':
shortcut.cpp:33:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i = 1;i<circle.size();i++){
| ~^~~~~~~~~~~~~~
shortcut.cpp:43:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i = 0;i<circle.size();i++){
| ~^~~~~~~~~~~~~~
shortcut.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | while(pt1 + 1 < circle.size() && (d[pt1+1] - d[i]) < circle_length - (d[pt1 + 1] - d[i])){
| ~~~~~~~~^~~~~~~~~~~~~~~
# | 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... |