CF 1073C. Vasya and Robot(二分)
阿新 • • 發佈:2018-12-17
題目連結
題目分析
二分,之前想多了,想成兩個方向沒有關係了,其實因為題目沒有問怎麼更改,可是我們可以想一下,我們判斷它無法到達的根據
因此只要任意兩個點之間的絕對值差要小於字元的長度都是可以辦到的
code
#include<bits/stdc++.h>
using namespace std;
#define MAX_VAL 1000005
#define MAX_ARRAY_SIZE 200005
#define ms(x,v) memset((x),(v),sizeof(x))
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define INF 0x3f3f3f3f
typedef long long LL;
typedef pair<int,int> Pair;
int n;
char s[MAX_ARRAY_SIZE];
int x,y;
int dx[MAX_ARRAY_SIZE];
int dy[MAX_ARRAY_SIZE];
int solve(int l){
int left = l-1 ,right = n+1;
while (left <= right) {
int mid = (left + right) >>1;
int X = dx[n] -(dx[mid] - dx[l-1]),Y = dy[n] - (dy[mid] - dy[l-1]);
if(abs(X - x) + abs(Y - y) <= mid - l+1)right= mid -1;
else left = mid +1;
}
return right +1;
}
int main (int argc, char const *argv[]) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> (s+1) >> x >> y;
if(abs(x) + abs(y) > n || (n - (abs(x)+abs(y))) %2 !=0){
std::cout << "-1" << '\n';
return 0;
}
dy[0] = dx[0] =0;
dy[n+1] = dx[n+1] = INF;
for(int i=1 ; i <=n ; ++i){
dx[i] = dx[i-1] +(s[i] =='R'?1 : (s[i]=='L'?-1 :0) );
dy[i] = dy[i-1] +(s[i] =='U'?1 : (s[i] == 'D'? -1 :0));
}
int ans = INF;
for(int i=1 ;i<=n ; ++i){
int last = solve(i);
if(last <=n)ans = min(ans , last - i +1);
}
std::cout << ans << '\n';
return 0;
}