题意:给你长度为n的字符串,每个字符为L, R, U, D。给你终点位置(x, y)。你每次出发的起点为(0, 0)。你可以改动每次的移动方向到达(x,y)点。求改动的MaxID-MinID+1是多少。

思路:

先分别求x轴,y轴上的前缀和,偏于之后判断区间是否满足条件。详细见代码。

固定左端点,二分枚举右端点。判断左右端点的区间是否能够达成目标(区间长度是否大于未完成量,奇偶性是否相同)

注意点:

strlen是O(n)的复杂度,超时了。

之前没遇到过固定一个点,然后另一个点二分逼近值的。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#define ll long long
#define local using namespace std; const int MOD = 1e9+;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 2e5+; int n;
ll endx, endy;
ll total;
char str[maxn];
int sumx[maxn];
int sumy[maxn]; bool ok(int l, int r) {
int nowx = sumx[n-]-sumx[r];
int nowy = sumy[n-]-sumy[r];
if (l > ) {
nowx += sumx[l-];
nowy += sumy[l-];
}
ll diff = abs(endx-nowx)+abs(endy-nowy);
if (diff <= r-l+ && ((diff&))==((r-l+)&)) return ;
else return ;
} int main() {
#ifdef local
if(freopen("/Users/Andrew/Desktop/data.txt", "r", stdin) == NULL) printf("can't open this file!\n");
#endif
scanf("%d", &n);
scanf("%s", str);
scanf("%lld%lld", &endx, &endy);
total = abs(endx)+abs(endy);//总的步数
//如果两者奇偶性不同也不行
if (total>n || ((total&) != (n&))) {
printf("-1\n");
return ;
}
sumx[] = ;
sumy[] = ;
int len = int(strlen(str));//strlen(str)是O(n)的复杂度 orz...
for (int i = ; i < len; ++i) {
int incx = ; int incy = ;
if (str[i] == 'U') {
incy = ;
} else if (str[i] == 'R') {
incx = ;
} else if (str[i] == 'D') {
incy = -;
} else {
incx = -;
}
if (i) {
sumx[i] += sumx[i-]+incx;
sumy[i] += sumy[i-]+incy;
}
else {
sumx[i] = incx;
sumy[i] = incy;
}
}
if (sumx[n-]==endx && sumy[n-]==endy) {
printf("0\n");
return ;
}
int mn = inf;
//枚举点
for (int i = ; i < n; ++i) {
int l = i-; int r = n-;
while (r-l > ) {
int m = (l+r)>>;
if (ok(i, m)) {
r = m;
} else {
l = m;
}
}
//判断一下r是否可行
if (ok(i, r)) mn = min(mn, r-i+);
}
printf("%d\n", mn);
#ifdef local
fclose(stdin);
#endif
return ;
}

CodeForce Educational round Div2 C - Vasya and Robot的更多相关文章

  1. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

    任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...

  2. CF Educational Round 78 (Div2)题解报告A~E

    CF Educational Round 78 (Div2)题解报告A~E A:Two Rival Students​ 依题意模拟即可 #include<bits/stdc++.h> us ...

  3. [CodeForces]Educational Round 52

    幸好我没有打这场,我VP的时候在C题就卡死了,我果然还是太菜了. A Vasya and Chocolate 题意:一个巧克力\(c\)元,买\(a\)赠\(b\),一共有\(n\)元,问能买几个巧克 ...

  4. [Educational Round 5][Codeforces 616F. Expensive Strings]

    这题调得我心疲力竭...Educational Round 5就过一段时间再发了_(:з」∠)_ 先后找了三份AC代码对拍,结果有两份都会在某些数据上出点问题...这场的数据有点水啊_(:з」∠)_[ ...

  5. [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]

    这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...

  6. CF 1073C Vasya and Robot(二分答案)

    C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. Codeforces Educational Round 33 题解

    题目链接   Codeforces Educational Round 33 Problem A 按照题目模拟,中间发现不对就直接输出NO. #include <bits/stdc++.h> ...

  8. Codeforces Educational Round 92 赛后解题报告(A-G)

    Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...

  9. Codeforces 1073C:Vasya and Robot(二分)

    C. Vasya and Robot time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard ...

随机推荐

  1. vue搭建

    ~感觉再不学点新技能,马上就要被淘汰了== 1.先安装node,直接点击下一步就OK  node官网   https://nodejs.org/en/ 2.安装完成之后,打开命令行工具,快捷键是cmd ...

  2. Devexpress GridControl 多选

    以前dev的多选要自己处理,一般的处理方式就是在单元格中添加checkbox控件.后来的版本中dev增加了多选的支持,只需要设置一下属性就可以了,属性如下图: 然后效果设计页面就是这个样子: 运行以后 ...

  3. Cocos2dx 代码中包含中文导致编译错误的问题解决方法

    从网上下载一个cocos2dx的源码,是IOS版本的,我将其迁移到windows 7下 ,用VS2010编译,出现一堆的C2001错误: 1>d:\cocos2d-x-2.2.6\mygame\ ...

  4. 一些常见的“功能性”JS事件

    ————————倒序上升———————— (2)给一些 保存.提交 按钮,添加 防止重复提交 事件(常见一例) $("#submitRank").attr("disabl ...

  5. Problem 5: Smallest multiple

    2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any rema ...

  6. 解决sublime text 3使用Install Package时出现There are no packages available for installation问题

    package control一直弹出There are no packages available for installation,由于国内环境屏蔽了https://packagecontrol. ...

  7. Swing学习1——总体概述

    以下来自于JDK1.6 一.Swing学习我划分为两个方面: 一方面Swing的界面设计部分,包括相关组件类的继承关系,组件的功能用途,布局管理: 1.首先继承关系上自上而下为 java.lang.O ...

  8. 巧用std::shared_ptr全局对象释放单例内存

    巧用std::shared_ptr 单例的使用相对比较广泛,但是需要在程序退出前调用它的析构函数对数据进行释放,常规做法是在main函数末尾进行释放工作, 但是这样相对比较繁琐,因此便有了利用全局变量 ...

  9. callback回调函数的理解

    callback采用的设计模式是:模板模式,他的设计理念是基于面向对象中的多态的. 我们的程序中走到某个地方他会出现不一样的动作的时候,我们在这儿就使用回调函数.我们利用的就是 多态的原理,我们传递不 ...

  10. 基于testng框架的web自动化测试

    package baidutest; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.o ...