|
| 1 | +# 题目链接 |
| 2 | + |
| 3 | +[C. Vasya and Robot](http://codeforces.com/contest/1073/problem/C) |
| 4 | + |
| 5 | +# 题目分析 |
| 6 | + |
| 7 | +二分,之前想多了,想成两个方向没有关系了,其实因为题目没有问怎么更改,可是我们可以想一下,我们判断它无法到达的根据 |
| 8 | + |
| 9 | +$n < abs(x) + abs(y) || (n - abs(x) - abs(y))\%2 ==1$ |
| 10 | + |
| 11 | +**因此只要任意两个点之间的绝对值差要小于字符的长度都是可以办到的** |
| 12 | + |
| 13 | +# code |
| 14 | + |
| 15 | +```cpp |
| 16 | +#include<bits/stdc++.h> |
| 17 | +using namespace std; |
| 18 | + |
| 19 | +#define MAX_VAL 1000005 |
| 20 | +#define MAX_ARRAY_SIZE 200005 |
| 21 | +#define ms(x,v) memset((x),(v),sizeof(x)) |
| 22 | +#define pb push_back |
| 23 | +#define fi first |
| 24 | +#define se second |
| 25 | +#define mp make_pair |
| 26 | +#define INF 0x3f3f3f3f |
| 27 | + |
| 28 | + |
| 29 | +typedef long long LL; |
| 30 | +typedef pair<int,int> Pair; |
| 31 | + |
| 32 | +int n; |
| 33 | +char s[MAX_ARRAY_SIZE]; |
| 34 | +int x,y; |
| 35 | +int dx[MAX_ARRAY_SIZE]; |
| 36 | +int dy[MAX_ARRAY_SIZE]; |
| 37 | + |
| 38 | +int solve(int l){ |
| 39 | + int left = l-1 ,right = n+1; |
| 40 | + while (left <= right) { |
| 41 | + int mid = (left + right) >>1; |
| 42 | + int X = dx[n] -(dx[mid] - dx[l-1]),Y = dy[n] - (dy[mid] - dy[l-1]); |
| 43 | + if(abs(X - x) + abs(Y - y) <= mid - l+1)right= mid -1; |
| 44 | + else left = mid +1; |
| 45 | + } |
| 46 | + return right +1; |
| 47 | +} |
| 48 | +int main (int argc, char const *argv[]) { |
| 49 | + ios_base::sync_with_stdio(0); |
| 50 | + cin.tie(0); |
| 51 | + |
| 52 | + cin >> n >> (s+1) >> x >> y; |
| 53 | + if(abs(x) + abs(y) > n || (n - (abs(x)+abs(y))) %2 !=0){ |
| 54 | + std::cout << "-1" << '\n'; |
| 55 | + return 0; |
| 56 | + } |
| 57 | + dy[0] = dx[0] =0; |
| 58 | + dy[n+1] = dx[n+1] = INF; |
| 59 | + for(int i=1 ; i <=n ; ++i){ |
| 60 | + dx[i] = dx[i-1] +(s[i] =='R'?1 : (s[i]=='L'?-1 :0) ); |
| 61 | + dy[i] = dy[i-1] +(s[i] =='U'?1 : (s[i] == 'D'? -1 :0)); |
| 62 | + } |
| 63 | + int ans = INF; |
| 64 | + for(int i=1 ;i<=n ; ++i){ |
| 65 | + int last = solve(i); |
| 66 | + if(last <=n)ans = min(ans , last - i +1); |
| 67 | + } |
| 68 | + std::cout << ans << '\n'; |
| 69 | + return 0; |
| 70 | +} |
| 71 | + |
| 72 | +``` |
0 commit comments