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 "rail.h"
#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
#include <map>
const int INF = 0x3f3f3f3f;
bool ckmin(auto& a, const auto& b) {return b<a?a=b,1:0;}
struct appl // apparent location
{
public:
int i, x;
appl(int _i=-1, int _x=0): i(_i), x(_x) {}
bool operator < (const appl& o) const {return x < o.x;}
bool operator > (const appl& o) const {return x > o.x;}
};
void findLocation(int N, int first, int location[], int stype[])
{
int A=0, B, bestd=INF;
std::map<int, int> map;
auto add=[&](int u){map[location[u]]=stype[u];};
std::vector<int> da(N), db(N);
for(int i=1;i<N;++i)
{
da[i]=getDistance(A, i);
if(ckmin(bestd, da[i]))
B=i;
}
location[A] = first;
stype[A] = 1;
add(A);
location[B] = first + da[B];
stype[B] = 2;
add(B);
std::vector<appl> left, right;
int AB = da[B];
int dbmin = da[B];
for(int i=0;i<N;++i)
if(i != A && i != B)
{
db[i] = getDistance(B, i);
ckmin(dbmin, db[i]);
if(db[i] == da[i]-AB && db[i] < AB)
location[i] = location[B]-db[i], stype[i]=1, add(i);
else // outside range: to left or to right?
{
if(db[i] == da[i]-AB) // left
left.push_back({i, location[B]-db[i]});
else
right.push_back({i, location[A]+da[i]});
}
}
std::sort(left.begin(), left.end(), std::greater<appl>());
std::sort(right.begin(), right.end());
{
appl prev(-1,0);
int v;
auto it = map.end();
for(auto n:left)
{
bool ok=0;
if(prev.i==-1 ||
(v=getDistance(prev.i, n.i))+prev.x >= location[A] ||
(it=map.find((v+prev.x+n.x)/2))==map.end() || // always even
it->second != 1)
{
prev=n;
location[n.i]=n.x;
stype[n.i]=1;
add(n.i);
}
else
{
location[n.i]=prev.x+v;
stype[n.i]=2;
add(n.i);
}
}
}
{
appl prev(-1,0);
int v;
auto it=map.end();
for(auto n:right)
if(prev.i==-1 ||
prev.x-(v=getDistance(prev.i, n.i)) <= location[B] ||
(it=map.find((prev.x-v+n.x)/2)) == map.end() || // always even
it->second != 2)
{
prev=n;
location[n.i]=n.x;
stype[n.i]=2;
add(n.i);
}
else
{
location[n.i]=prev.x-v;
stype[n.i]=1;
add(n.i);
}
}
//for(int i=0;i<N;++i) printf("%d %d\n", stype[i], location[i]);
}
Compilation message (stderr)
rail.cpp:10:12: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
10 | bool ckmin(auto& a, const auto& b) {return b<a?a=b,1:0;}
| ^~~~
rail.cpp:10:27: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
10 | bool ckmin(auto& a, const auto& b) {return b<a?a=b,1:0;}
| ^~~~
rail.cpp: In function 'void findLocation(int, int, int*, int*)':
rail.cpp:68:9: warning: unused variable 'ok' [-Wunused-variable]
68 | bool ok=0;
| ^~
rail.cpp:37:28: warning: 'B' may be used uninitialized in this function [-Wmaybe-uninitialized]
37 | location[B] = first + da[B];
| ^
# | 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... |