Educational Codeforces Round 53 (Rated for Div. 2) C Vasya and Robot 二分

时间:2023-03-10 06:17:03
Educational Codeforces Round 53 (Rated for Div. 2) C Vasya and Robot 二分

题目:题目链接

思路:对于x方向距离与y方向距离之和大于n的情况是肯定不能到达的,另外,如果n比abs(x) + abs(y)大,那么我们总可以用UD或者LR来抵消多余的大小,所以只要abs(x) + abs(y) <= n && (n - abs(x) + abs(y)) % 2 == 0,就一定可以到达终点,判断完之后二分答案长度就可以了

AC代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <cstdlib>
#include <cmath> #define FRER() freopen("in.txt", "r", stdin)
#define FREO() freopen("out.txt", "w", stdout)
#define INF 0x3f3f3f3f using namespace std; const int maxn = + ; int n, cx[maxn], cy[maxn], x, y;
char str[maxn]; bool judge(int t) {
for(int i = t; i <= n; ++i) {
int lx = cx[n] - cx[i] + cx[i - t];
int ly = cy[n] - cy[i] + cy[i - t];
if (abs(x - lx) + abs(y - ly) <= t)
return true;
}
return false;
} int main()
{
cin >> n >> str >> x >> y;
if(abs(x) + abs(y) > n || (abs(x) + abs(y)) % != n % ) {
cout << - << endl;
}
else {
for(int i = ; i < n; ++i) {
if(str[i] == 'U') cx[i + ] = cx[i], cy[i + ] = cy[i] + ;
else if(str[i] == 'D') cx[i + ] = cx[i], cy[i + ] = cy[i] - ;
else if(str[i] == 'L') cx[i + ] = cx[i] - , cy[i + ] = cy[i];
else if(str[i] == 'R') cx[i + ] = cx[i] + , cy[i + ] = cy[i];
}
int l = , r = n;
while(l < r) {
int m = (l + r) >> ;
if(judge(m))
r = m;
else
l = m + ;
}
cout << r << endl;
}
return ;
}