Advertisement
ismaeil

UVa 10022

Jun 7th, 2021
1,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef pair<int , int> ii;
  6. #define SQ(a) (a) * (a)
  7. #define S second
  8. #define F first
  9.  
  10. ii getCord(int x)
  11. {
  12.     int row = ceil(sqrt(x));
  13.  
  14.     int mnRow = SQ(row - 1) + 1;
  15.     int mxRow = SQ(row);
  16.     int mdRow = (mxRow + mnRow) / 2;
  17.  
  18.     int col = x - mdRow;
  19.  
  20.     return ii(row ,col);
  21. }
  22.  
  23. bool canDown(int x)
  24. {
  25.     int row = ceil(sqrt(x));
  26.     int col = x - SQ(row - 1);
  27.     return col % 2;
  28. }
  29.  
  30. int getAns(ii a ,ii b ,int A ,int B)
  31. {
  32.     int colDiff = abs(b.S - a.S);
  33.     int rowDiff = abs(b.F - a.F);
  34.  
  35.     int ans = rowDiff + colDiff;
  36.  
  37.     if( colDiff <= rowDiff )
  38.     {
  39.         ans = 2 * rowDiff;
  40.        
  41.         if( !canDown(A) ) ans += 1;
  42.         if( !canDown(B) ) ans -= 1;
  43.     }
  44.  
  45.     return ans;
  46. }
  47.  
  48. void Solve()
  49. {
  50.     int a;  scanf("%d" ,&a);
  51.     int b;  scanf("%d" ,&b);
  52.  
  53.     if( a > b ) swap(a , b);
  54.  
  55.     printf("%d\n" ,getAns(getCord(a) ,getCord(b) ,a ,b));
  56. }
  57.  
  58. int main()
  59. {
  60.     int Tc;
  61.     scanf("%d" ,&Tc);
  62.  
  63.     bool f = true;
  64.     while( Tc-- )
  65.     {
  66.         if( !f ) puts("");
  67.         f = false;
  68.  
  69.         Solve();
  70.     }
  71.     return 0;
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement