Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

Time Limit: 2000 mSec

Problem Description

Input

Output

The only line should contain the minimal number of days required for the ship to reach the point (x2,y2)(x2,y2).

If it's impossible then print "-1".

Sample Input

0 0
4 6
3
UUU

Sample Output

5

题解:第一感觉是bfs,然后大概背包一下之类的,但是数据范围不允许呀,做出这个题的关键点就是注意到运动独立性(呵呵),和如果在第x天能到,那么对于y >= x的都能到,因此就可以二分,固定了天数之后,风吹的距离就是固定的,并且可以用前缀和O(1)求,然后就是判断风吹到的点和目标点的哈密顿距离是否<=天数即可。

扫描二维码关注公众号,回复: 6051380 查看本文章
 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define REP(i, n) for (int i = 1; i <= (n); i++)
 6 #define sqr(x) ((x) * (x))
 7 
 8 const int maxn = 100000 + 100;
 9 const int maxm = 200000 + 100;
10 const int maxs = 10000 + 10;
11 
12 typedef long long LL;
13 typedef pair<int, int> pii;
14 typedef pair<double, double> pdd;
15 
16 const LL unit = 1LL;
17 const int INF = 0x3f3f3f3f;
18 const double eps = 1e-14;
19 const double inf = 1e15;
20 const double pi = acos(-1.0);
21 const int SIZE = 100 + 5;
22 const LL MOD = 1000000007;
23 
24 LL sx, sy, ex, ey;
25 LL x[maxn], y[maxn];
26 LL n;
27 string str;
28 
29 bool Judge(LL lim)
30 {
31     LL m = lim / n, remain = lim % n;
32     LL xx = sx + m * x[n] + x[remain];
33     LL yy = sy + m * y[n] + y[remain];
34     if (abs(xx - ex) + abs(yy - ey) <= lim)
35         return true;
36     return false;
37 }
38 
39 int main()
40 {
41     ios::sync_with_stdio(false);
42     cin.tie(0);
43     //freopen("input.txt", "r", stdin);
44     //freopen("output.txt", "w", stdout);
45     cin >> sx >> sy >> ex >> ey;
46     cin >> n;
47     cin >> str;
48     LL len = str.size();
49     for (LL i = 0; i < len; i++)
50     {
51         if (str[i] == 'U')
52         {
53             x[i + 1] = x[i];
54             y[i + 1] = y[i] + 1;
55         }
56         else if (str[i] == 'D')
57         {
58             x[i + 1] = x[i];
59             y[i + 1] = y[i] - 1;
60         }
61         else if (str[i] == 'L')
62         {
63             x[i + 1] = x[i] - 1;
64             y[i + 1] = y[i];
65         }
66         else
67         {
68             x[i + 1] = x[i] + 1;
69             y[i + 1] = y[i];
70         }
71     }
72     LL le = 0, ri = LLONG_MAX / 10;
73     LL ans = -1;
74     while (le <= ri)
75     {
76         LL mid = (le + ri) / 2;
77         if (Judge(mid))
78         {
79             ri = mid - 1;
80             ans = mid;
81         }
82         else
83         {
84             le = mid + 1;
85         }
86     }
87     cout << ans << endl;
88 
89     return 0;
90 }

猜你喜欢

转载自www.cnblogs.com/npugen/p/10786503.html