Advertisement
pratiyush7

Nearest City Neighbour

Oct 30th, 2021
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. // Pratiyush Mishra
  2.  
  3. #include <bits/stdc++.h>
  4. #define ull unsigned long long int
  5. #define ll long long int
  6. #define LL_MAX 9223372036854775807
  7. #define pb push_back
  8. #define pf push_front
  9. #define mp make_pair
  10. #define popb pop_back
  11. #define vl vector<ll>
  12. #define pl pair<ll,ll>
  13. #define bs(v, x) binary_search(v.begin(), v.end(), x)
  14. #define mem1(a) memset(a, -1, sizeof(a))
  15. #define popf pop_front
  16. #define p0(x) cout << x << " "
  17. #define p1(x) cout << x << '\n'
  18. #define p2(x, y) cout << x << " " << y << '\n'
  19. #define p3(x, y, z) cout << x << " " << y << " " << z << '\n'
  20. #define printv(v)                   \
  21.   for (ll i = 0; i < v.size(); ++i) \
  22.     cout << v[i] << " ";            \
  23.   cout << '\n'
  24. #define pr1(x) cout << fixed << setprecision(15) << x << '\n'
  25. #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update>
  26. // ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
  27. // s.find_by_order(i)  itertor to ith element (0 indexed)
  28. #define mod 1000000007
  29. #define mod1 998244353
  30. #define fio                         \
  31.   ios_base::sync_with_stdio(false); \
  32.   cin.tie(NULL)
  33. #define get(n) \
  34.   ll n;        \
  35.   cin >> n
  36. #define getvec(v, n)         \
  37.   vector<ll> v(n);           \
  38.   for (ll i = 0; i < n; i++) \
  39.     cin >> v[i];
  40. #define getstr(s) \
  41.   string s;       \
  42.   cin >> s
  43. #define all(x) x.begin(), x.end()
  44. #define countBits(x) __builtin_popcount(x)
  45. using namespace std;
  46.  
  47. vector<int> x, y;
  48. map<int, vector<pair<int, string>>> dp_x, dp_y;
  49. map<string, int> inv_name;
  50. vector<string> name;
  51.  
  52. int find(vector<pair<int, string>> &v, int x)
  53. {
  54.   int l = 0;
  55.   int h = v.size() - 1;
  56.   int ans = -1;
  57.   while (l <= h)
  58.   {
  59.     int mid = (l + h) / 2;
  60.     int val = v[mid].first;
  61.     if (val > x)
  62.       h = mid - 1 ;
  63.     else if (val < x)
  64.       l = mid + 1;
  65.     else
  66.       return mid;
  67.   }
  68.   return ans;
  69. }
  70.  
  71. pair<int, string> cal(int pos, vector<pair<int, string>> &v)
  72. {
  73.   int dis = INT_MAX;
  74.   string ans = "";
  75.   for (int j = -1; j <= 1; j += 2)
  76.   {
  77.     int n_pos = pos + j;
  78.     if (n_pos >= 0 && n_pos < v.size())
  79.     {
  80.       int diff = abs(v[pos].first - v[n_pos].first);
  81.       if (diff <= dis)
  82.       {
  83.         dis = diff;
  84.         if (ans == "")
  85.           ans = v[n_pos].second;
  86.         else if (ans > v[n_pos].second)
  87.           ans = v[n_pos].second;
  88.       }
  89.     }
  90.   }
  91.   return {dis, ans};
  92. }
  93.  
  94. void solve(string &s)
  95. {
  96.   int X = x[inv_name[s]];
  97.   int Y = y[inv_name[s]];
  98.   int pos_x = find(dp_x[X], X);
  99.   int pos_y = find(dp_y[Y], Y);
  100.   string ans = "";
  101.   pair<int, string> it1 = cal(pos_x, dp_x[X]);
  102.   pair<int, string> it2 = cal(pos_y, dp_y[Y]);
  103.   if (it1.first == INT_MAX && it2.first == INT_MAX)
  104.     p1("None");
  105.   else if (it1.first == INT_MAX)
  106.     p1(it2.second);
  107.   else if (it2.first == INT_MAX)
  108.     p1(it1.second);
  109.   else if (it1.first > it2.first)
  110.     p1(it2.second);
  111.   else if (it1.first < it2.first)
  112.     p1(it1.second);
  113.   else
  114.     p1(min(it1.second, it2.second));
  115. }
  116.  
  117. void mainSolve()
  118. {
  119.   int n;
  120.   cin >> n;
  121.   x.clear();
  122.   y.clear();
  123.   name.clear();
  124.   dp_x.clear();
  125.   dp_y.clear();
  126.   inv_name.clear();
  127.   for (int i = 0; i < n; i++)
  128.   {
  129.     int X;
  130.     cin >> X;
  131.     x.push_back(X);
  132.   }
  133.   for (int i = 0; i < n; i++)
  134.   {
  135.     int Y;
  136.     cin >> Y;
  137.     y.push_back(Y);
  138.   }
  139.   for (int i = 0; i < n; ++i)
  140.   {
  141.     string s;
  142.     cin >> s;
  143.     name.push_back(s);
  144.     inv_name[s] = i;
  145.   }
  146.   for (int i = 0; i < n; i++)
  147.   {
  148.     dp_x[x[i]].push_back({y[i], name[i]});
  149.     dp_y[y[i]].push_back({x[i], name[i]});
  150.   }
  151.   for (auto &it : dp_x)
  152.   {
  153.     sort(it.second.begin(), it.second.end());
  154.   }
  155.   for (auto &it : dp_y)
  156.   {
  157.     sort(it.second.begin(), it.second.end());
  158.   }
  159.   int q;
  160.   cin >> q;
  161.   while (q--)
  162.   {
  163.     string s;
  164.     cin >> s;
  165.     solve(s);
  166.   }
  167. }
  168.  
  169. int main()
  170. {
  171.   fio;
  172. #ifndef ONLINE_JUDGE
  173.   freopen("input.txt", "r", stdin);
  174.   freopen("output.txt", "w", stdout);
  175. #endif
  176.   get(t);
  177.   //ll t = 1;
  178.   while (t--)
  179.   {
  180.     mainSolve();
  181.   }
  182.   return 0;
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement