C. Vasya and Robot (two points)

Title: Portal

The meaning of problems: first you in (0, 0), you want the point (x, y), you have a character string length n of motion, the string L, R, U, D composition. After asking you how you need to change the string, the string so that you finish this sport, just before the (x, y) and the need MaxID (maximum subscript characters that your changes) - Minimum MinID (characters that you change the subscript) + 1 the minimum value, the output of this minimum, if not in any way to the (x, y) outputs -1.

 

Solution: half length needs to be changed, change is certainly consecutive len characters, and then enumerate changes range. Performing an operation on the outer section changes, it is determined, and the parity of the number of steps required and if len is the same or less number of steps required len.

 

#include <bits/stdc++.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 1e6 + 5;

char a[N];
int n;
int x, y;

void update(pair <int, int>& Q, char s, int x) {
    if(s == 'U') Q.second += x;
    else if(s == 'D') Q.second -= x;
    else if(s == 'L') Q.first -= x;
    else if(s == 'R') Q.first += x;
}

bool judge(int len) {
    pair <int, int> tmp = make(0, 0);
    rep(i, len + 1, n) update(tmp, a[i], 1);

    int l = 1, r = len + 1;
    while(1) {
        int now = abs(x - tmp.first) + abs(y - tmp.second);
        if((now % 2) == (len % 2) && len >= now) return true;
        if(r == n + 1) break;
        update(tmp, a[l++], 1);
        update(tmp, a[r++], -1);
    }
    return false;
}

void solve() {
    scanf("%d", &n);
    scanf("%s", a + 1);
    scanf("%d %d", &x, &y);if(!judge(n)) { puts("-1"); return ; }
    int l = -1, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if(judge(mid)) r = mid;
        else l = mid;
    }
    printf("%d\n", r);
    return ;
}

int main() {

    solve();

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Willems/p/12388568.html