题意:给你长度为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. 作业:K-means算法应用:图片压缩

    from sklearn.datasets import load_sample_image from sklearn.cluster import KMeans import matplotlib. ...

  2. 其他知识点,API

    日期和Math 获取2017-06-10格式的日期 Date.now() //当前时间毫秒数 var dt=new Date() dt.getTime() //毫秒数 dt.getFullYear() ...

  3. C语言的#if #ifdef #ifndef

    #if #ifedf #ifndef   —般情况下,C语言源程序中的每一行代码.都要参加编译.但有时候出于对程序代码优化的考虑.希望只对其中一部分内容进行编译.此时就需要在程序中加上条件,让编译器只 ...

  4. 七月在线爬虫班学习笔记(六)——scrapy爬虫整体示例

    第六课主要内容: 爬豆瓣文本例程 douban 图片例程 douban_imgs 1.爬豆瓣文本例程 douban 目录结构 douban --douban --spiders --__init__. ...

  5. ykit入门

    init  lint pack server  watch 1.创建现有工程的ykit项目 在项目文件夹下 ykit init 2.lint 检查当前项目的代码质量 手动执行代码 可验证代码正误 yk ...

  6. SQLZOO网页中SQL的答案(SELECT from nobel篇)

    SELECT from nobel篇 1. 更改查詢以顯示1950年諾貝爾獎的獎項資料. 答案: SELECT yr, subject, winner FROM nobel WHERE yr = 19 ...

  7. EXCEL查找函数之VLOOKUP,LOOKUP,HLOOKUP

    VLOOKUP是纵向查询函数,VLOOKUP(lookup_value,table_array,col_index_num,range_lookup). 参数                      ...

  8. POST 调用 301 Moved Permanently 问题

    如上测试了2种,第一个post访问接口https能正常返回,第二个post访问接口http时301 Moved Permanently出现永久重定向问题,经检查发现服务器nginx对80端口做了重定向 ...

  9. echarts设置option中的数据对象优化

    if(tab.name == 'first'){ myChart.setOption({ legend: { selected:{ [this.playNumber]:true, [this.cove ...

  10. springboot整合mybatics PLUS

    首先添加maven依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactI ...