Task: | Labyrintti |
Sender: | |
Submission time: | 2015-10-09 18:13:16 +0300 |
Language: | C++ |
Status: | READY |
Result: | 100 |
group | verdict | score |
---|---|---|
#1 | ACCEPTED | 12 |
#2 | ACCEPTED | 29 |
#3 | ACCEPTED | 59 |
test | verdict | time | group | |
---|---|---|---|---|
#1 | ACCEPTED | 0.06 s | 1 | details |
#2 | ACCEPTED | 0.06 s | 1 | details |
#3 | ACCEPTED | 0.05 s | 1 | details |
#4 | ACCEPTED | 0.06 s | 1 | details |
#5 | ACCEPTED | 0.06 s | 1 | details |
#6 | ACCEPTED | 0.05 s | 2 | details |
#7 | ACCEPTED | 0.07 s | 2 | details |
#8 | ACCEPTED | 0.06 s | 2 | details |
#9 | ACCEPTED | 0.05 s | 2 | details |
#10 | ACCEPTED | 0.05 s | 2 | details |
#11 | ACCEPTED | 0.05 s | 3 | details |
#12 | ACCEPTED | 0.05 s | 3 | details |
#13 | ACCEPTED | 0.06 s | 3 | details |
#14 | ACCEPTED | 0.05 s | 3 | details |
#15 | ACCEPTED | 0.06 s | 3 | details |
Code
#include <stdint.h> #include <stdlib.h> #include <algorithm> #include <iostream> #include <vector> typedef std::pair<int64_t,int64_t>point; inline int64_t& X(point& p){return p.first;} inline int64_t& Y(point& p){return p.second;} bool within(point p,std::pair<point,point>bounds){ auto r1=bounds.first,r2=bounds.second; return X(p)>std::min(X(r1),X(r2))&&X(p)<std::max(X(r1),X(r2))&& Y(p)>std::min(Y(r1),Y(r2))&&Y(p)<std::max(Y(r1),Y(r2)); } point spiral(int i){ if(i<=0)return{0,0}; static std::vector<point>spiral{{0,0}}; static const std::vector<int>dx{1,0,-1,0},dy{0,-1,0,1}; for(int j=spiral.size()-1;j<i;++j){ int64_t d=2ULL<<j; spiral.emplace_back( X(spiral[j])+d*dx[j%4], Y(spiral[j])+d*dy[j%4]); } return spiral[i]; } std::pair<point,point>box(int i){ point p[3]{spiral(i),spiral(i-1),spiral(i-4)}; int64_t xs[3]{X(p[0]),X(p[1]),X(p[2])}; int64_t ys[3]{Y(p[0]),Y(p[1]),Y(p[2])}; auto min3=[](int64_t a[]){return std::min(std::min(a[0],a[1]),a[2]);}; auto max3=[](int64_t a[]){return std::max(std::max(a[0],a[1]),a[2]);}; return {{min3(xs),min3(ys)},{max3(xs),max3(ys)}}; } point corner(int i){ static const std::vector<int>dx{-1,1,1,-1},dy{1,1,-1,-1}; auto p=spiral(i-4); return{X(p)+dx[i%4],Y(p)+dy[i%4]}; } int64_t manhattan(point a,point b){ return ::llabs(X(a)-X(b))+::llabs(Y(a)-Y(b)); } int main(){ point A,B;std::cin>>Y(A)>>X(A)>>Y(B)>>X(B); int64_t distance=0; bool foundA=false; for(int i=3;;++i){ auto b=box(i); if(within(A,b)&&within(B,b))return std::cout<<manhattan(A,B)<<'\n',0; if(foundA){ if(within(B,b))return std::cout<<distance+manhattan(B,corner(i-1))<<'\n',0; distance+=manhattan(corner(i-1),corner(i)); } else{ if(within(B,b))std::swap(A,B); if(within(A,b)){ foundA=true; distance=manhattan(A,corner(i)); } } } }
Test details
Test 1
Group: 1
Verdict: ACCEPTED
input |
---|
6 -1 8 -2 |
correct output |
---|
3 |
user output |
---|
3 |
Test 2
Group: 1
Verdict: ACCEPTED
input |
---|
-8 2 8 -9 |
correct output |
---|
27 |
user output |
---|
27 |
Test 3
Group: 1
Verdict: ACCEPTED
input |
---|
3 2 -5 -1 |
correct output |
---|
13 |
user output |
---|
13 |
Test 4
Group: 1
Verdict: ACCEPTED
input |
---|
4 4 2 7 |
correct output |
---|
5 |
user output |
---|
5 |
Test 5
Group: 1
Verdict: ACCEPTED
input |
---|
-5 4 -6 -3 |
correct output |
---|
8 |
user output |
---|
8 |
Test 6
Group: 2
Verdict: ACCEPTED
input |
---|
-156 226 -9 7 |
correct output |
---|
438 |
user output |
---|
438 |
Test 7
Group: 2
Verdict: ACCEPTED
input |
---|
403 265 10 0 |
correct output |
---|
1100 |
user output |
---|
1100 |
Test 8
Group: 2
Verdict: ACCEPTED
input |
---|
146 462 9 -3 |
correct output |
---|
1160 |
user output |
---|
1160 |
Test 9
Group: 2
Verdict: ACCEPTED
input |
---|
223 82 -1 5 |
correct output |
---|
725 |
user output |
---|
725 |
Test 10
Group: 2
Verdict: ACCEPTED
input |
---|
1 390 -5 2 |
correct output |
---|
436 |
user output |
---|
436 |
Test 11
Group: 3
Verdict: ACCEPTED
input |
---|
-540305713988379 -580514137141... |
correct output |
---|
1233409841814111 |
user output |
---|
1233409841814111 |
Test 12
Group: 3
Verdict: ACCEPTED
input |
---|
-156703691254895 -816797859965... |
correct output |
---|
1086091541904259 |
user output |
---|
1086091541904259 |
Test 13
Group: 3
Verdict: ACCEPTED
input |
---|
-438806811971369 -748477242640... |
correct output |
---|
1299874045296142 |
user output |
---|
1299874045296142 |
Test 14
Group: 3
Verdict: ACCEPTED
input |
---|
939351840049839 -9541207461566... |
correct output |
---|
2118652567575152 |
user output |
---|
2118652567575152 |
Test 15
Group: 3
Verdict: ACCEPTED
input |
---|
-840127817790022 1113515308437... |
correct output |
---|
1007774343975947 |
user output |
---|
1007774343975947 |