Advertisement
deadwing97

CATSRATS Author

Mar 25th, 2019
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. // Cats & Rats    equal-speed   OX-line
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. typedef long double LD;
  7. typedef long long int LL;
  8. typedef pair <LL,LL> Point;
  9.  
  10. #define X first
  11. #define Y second
  12.  
  13. const int maxn = 2000;
  14. const LL inf = 1e18;
  15. LL a[maxn], b[maxn], s[maxn], e[maxn];
  16.  
  17. LL secTime(Point A, Point B, Point C, Point D) {
  18.     // Line AB represented as a1x + b1y = c1
  19.     LD a1 = B.Y - A.Y, b1 = A.X - B.X;
  20.     LD c1 = a1 * A.X + b1 * A.Y;
  21.     // Line CD represented as a2x + b2y = c2
  22.     LD a2 = D.Y - C.Y, b2 = C.X - D.X;
  23.     LD c2 = a2 * C.X+ b2 * C.Y;
  24.     LD makh = a1 * b2 - a2 * b1;
  25.     LD sora = b2 * c1 - b1 * c2;
  26.     LD ret = (2 * sora) / makh;
  27.     LL upper = ret + 1e-9;
  28.     return upper;
  29. }
  30.  
  31. LL intersect(int i, int j) {
  32.     if (max(a[i], b[i]) < min(b[j], a[j]) || max(a[j], b[j]) < min(b[i], a[i]))
  33.         return inf;
  34.     if (e[i] < s[j] || e[j] < s[i])
  35.         return inf;
  36.  
  37.     if (a[i] < b[i] && a[j] < b[j]) {
  38.         if (a[i] > a[j])
  39.             swap(i, j);
  40.         if (s[j] == s[i] + (a[j] - a[i]))
  41.             return 2 * s[j];
  42.         return inf;
  43.     }
  44.  
  45.     if (a[i] > b[i] && a[j] > b[j]) {
  46.         if (a[i] > a[j])
  47.             swap(i, j);
  48.         if (s[i] == s[j] + (a[j] - a[i]))
  49.             return 2 * s[i];
  50.         return inf;
  51.     }
  52.     Point A = make_pair(s[i], a[i]);
  53.     Point B = make_pair(e[i], b[i]);
  54.     Point C = make_pair(s[j], a[j]);
  55.     Point D = make_pair(e[j], b[j]);
  56.     LL ret = secTime(A, B, C, D);
  57.     if (s[i] * 2 > ret || ret > e[i] * 2 || s[j] * 2 > ret || ret > e[j] * 2)
  58.         return inf;
  59.     return ret;
  60. }
  61.  
  62. int main() {
  63.     ios_base::sync_with_stdio(false);
  64.     int tc, n, m;
  65.     cin >> tc;
  66.     while (tc--) {
  67.         cin >> n >> m;
  68.         for (int i = 0; i < n; i++) {
  69.             cin >> a[i] >> b[i] >> s[i];
  70.             e[i] = s[i] + abs(b[i] - a[i]);
  71.         }
  72.         for (int i = n; i < n + m; i++) {
  73.             cin >> a[i] >> b[i] >> s[i];
  74.             e[i] = s[i] + abs(b[i] - a[i]);
  75.         }
  76.  
  77.         for (int i = n; i < n + m; i++) {
  78.             int idx = -1;
  79.             LL mint = inf;
  80.             for (int j = 0; j < n; j++)
  81.                 if (mint > intersect(i, j))
  82.                     mint = intersect(i, j), idx = j + 1;
  83.             cout << idx << ' ';
  84.         }
  85.         cout << '\n';
  86.     }
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement