Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define x first
- #define y second
- using namespace std;
- typedef long long ll;
- ll x, y, x2, y2;
- ll n;
- char a[100010];
- ll col[1010];
- ll chi[100][100010];
- ll ans = 0;
- ll tx[100010], ty[100010];
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cin >> x >> y;
- cin >> x2 >> y2;
- cin >> n;
- for (int i = 1; i <= n; ++i) cin >> a[i];
- ll dx = x2 - x, dy = y2 - y;
- ll lx = 0, ly = 0;
- for (int i = 1; i <= n; ++i)
- {
- if (a[i] == 'L') --lx;
- if (a[i] == 'R') ++lx;
- if (a[i] == 'U') ++ly;
- if (a[i] == 'D') --ly;
- tx[i] = lx;
- ty[i] = ly;
- }
- ll l = 0, r = 1e18;
- while (r - l > 1)
- {
- ll m = (r + l) / 2;
- ll k = m / n;
- if (abs(dx - tx[n] * k - tx[m % n]) + abs(dy - ty[n] * k - ty[m % n]) > m)
- {
- l = m;
- } else
- {
- r = m;
- }
- }
- ll m = l;
- ll k = m / n;
- if (abs(dx - tx[n] * k - tx[m % n]) + abs(dy - ty[n] * k - ty[m % n]) > m)
- {
- m = r;
- ll k = m / n;
- if (abs(dx - tx[n] * k - tx[m % n]) + abs(dy - ty[n] * k - ty[m % n]) > m)
- {
- cout << -1;
- } else
- {
- cout << r;
- }
- } else
- {
- cout << l;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement