1117C - Magic Ship - CodeForces Solution


binary search *1900

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <tuple>
#include <cmath>
#include <sstream>
#include <bitset>
#include <list>
#include <cstring>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef vector<vs> vvs;
typedef vector<pii> vpii;
typedef vector<tiii> vtiii;

#define test int tt; scanf("%d", &tt); while (tt--)
#define forn(i, r) for (int (i) = 0; (i) < (r); ++(i))
#define loop(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define ep emplace_back
#define ff first 
#define ss second
#define all(x) (x).begin(), (x).end()
#define len(x) (int)(x).size()
#define UNIQUE(x) (x).resize(unique(all(x))-(x).begin())
#define LSOne(S) (S & (-S))

#define oo (int)1e9
#define ooo (ll)1e18

void fast() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
}

map<char, int> idx = {{'U', 0}, {'D', 1}, {'R', 2}, {'L', 3}};

vl dir_x = {0, 0, 1, -1};
vl dir_y = {1, -1, 0, 0};

ll sx, sy, tx, ty, n;
string s;
vl dx, dy;

bool f(ll k) {
    ll x = sx, y = sy;
    
    ll c = k / n, r = k % n;
    x += c * dx[n] + dx[r];
    y += c * dy[n] + dy[r];
    
    return abs(x-tx) + abs(y-ty) <= k;
}

int main() {
    cin >> sx >> sy >> tx >> ty >> n >> s;
    
    dx.resize(n+1, 0);
    dy.resize(n+1, 0);
    
    for (int i = 0; i < n; i++) {
        dx[i+1] = dx[i] + dir_x[idx[s[i]]];
        dy[i+1] = dy[i] + dir_y[idx[s[i]]];
    }
    
    ll l = 0, r = (ll)1e18;
    
    while (r - l > 1) {
        ll mid = l + (r - l) / 2;
        if (f(mid)) r = mid;
        else l = mid;
    }
    
    if (f(r)) cout << r << endl;
    else cout << -1 << endl;
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array