题意:给定长为n的机器人行走路线,每个字符代表上下左右走,可以更改将一些字符改成另外三个字符,定义花费为更改的下标max-min+1,

问从(0,0)走到(X,Y)的最小花费,无解输出-1

n<=2e5,abs(X),abs(Y)<=1e9

思路:第一反应是二分,但其实并没有这个取到等号的严格的性质

不过因为内部可以调整,我们还是可以二分长度,然后看内部调整能不能构造出一组可行解

 #include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define N 210000
#define M 1100
#define MOD 2147493647
#define eps 1e-8
#define pi acos(-1) char ch[N];
int s[N][],n;
ll x[N],y[N],X,Y; int judge(int s1,int s2,int s3,int s4,int X,int Y)
{
int tx=s4-s3;
int ty=s1-s2;
while(tx<X)
{
if(s3>&&tx+<=X)
{
s3--; s4++;
tx+=;
continue;
}
if(s1>&&tx+<=X)
{
s1--; s4++;
ty--;
tx++;
continue;
}
if(s2>&&tx+<=X)
{
s2--; s4++;
ty++;
tx++;
continue;
}
break;
} while(tx>X)
{
if(s4>&&tx->=X)
{
s4--; s3++;
tx-=;
continue;
}
if(s1>&&tx->=X)
{
s1--; s3++;
ty--;
tx--;
continue;
}
if(s2>&&tx->=X)
{
s2--; s3++;
ty++;
tx--;
continue;
}
break;
}
if(tx!=X) return ;
if(abs(Y-ty)%) return ;
return ;
} int isok(int len)
{
for(int i=;i<=n-len+;i++)
{
int j=i+len-;
ll tx=x[i-]+x[n]-x[j];
ll ty=y[i-]+y[n]-y[j];
ll t=abs(X-tx)+abs(Y-ty);
int s1=s[j][]-s[i-][];
int s2=s[j][]-s[i-][];
int s3=s[j][]-s[i-][];
int s4=s[j][]-s[i-][];
if(t==len||t<len&&judge(s1,s2,s3,s4,X-tx,Y-ty)) return ;
}
return ;
} int main()
{
scanf("%d",&n);
scanf("%s",ch+);
scanf("%lld%lld",&X,&Y);
x[]=y[]=;
s[][]=s[][]=s[][]=s[][]=;
for(int i=;i<=n;i++)
{
x[i]=x[i-];
y[i]=y[i-];
for(int j=;j<=;j++) s[i][j]=s[i-][j];
if(ch[i]=='U'){y[i]++; s[i][]++;}
if(ch[i]=='D'){y[i]--; s[i][]++;}
if(ch[i]=='L'){x[i]--; s[i][]++;}
if(ch[i]=='R'){x[i]++; s[i][]++;}
}
int l=;
int r=n;
int last=n+;
while(l<=r)
{
int mid=(l+r)>>;
if(isok(mid)){last=mid; r=mid-;}
else l=mid+;
}
if(last==n+) printf("-1\n");
else printf("%d\n",last);
return ;
}

【CF1073C】Vasya and Robot(二分,构造)的更多相关文章

  1. C. Vasya and Robot二分

    1.题目描述 Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell ...

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

    题目:题目链接 思路:对于x方向距离与y方向距离之和大于n的情况是肯定不能到达的,另外,如果n比abs(x) + abs(y)大,那么我们总可以用UD或者LR来抵消多余的大小,所以只要abs(x) + ...

  3. CF1073C Vasya and Robot

    CF题目难度普遍偏高啊-- 一个乱搞的做法.因为代价为最大下标减去最小的下标,那么可以看做一个区间的修改.我们枚举选取的区间的右端点,不难发现满足条件的左端点必然是不降的.那么用一个指针移一下就好了 ...

  4. 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 ...

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

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

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

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

  7. 【Atcoder】AGC 020 D - Min Max Repetition 二分+构造

    [题意]定义f(A,B)为一个字符串,满足: 1.长度为A+B,含有A个‘A',B个'B'. 2.最长的相同字符子串最短. 3.在满足以上2条的情况下,字典序最小. 例如, f(2,3) = BABA ...

  8. Codeforces 1073C Vasya and Robot 【二分】

    <题目链接> 题目大意: 一个机器人从(0,0)出发,输入一段指令字符串,和机器人需要在指定步数后到达的终点,问如果机器人需要在指定步数内到达终点,那么需要对原指令字符串做出怎样的改变,假 ...

  9. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot(二分或者尺取)

    题目哦 题意:给出一个序列,序列有四个字母组成,U:y+1,D:y-1 , L:x-1 , R:x+1;   这是规则 . 给出(x,y) 问可不可以经过最小的变化这个序列可以由(0,0) 变到(x, ...

随机推荐

  1. Java 批量文件压缩导出,并下载到本地

    主要用的是org.apache.tools.zip.ZipOutputStream  这个zip流,这里以Execl为例子. 思路首先把zip流写入到http响应输出流中,再把excel的流写入zip ...

  2. Redis之Hash类型操作

    接口IRedisDaoHash: package com.net.test.redis.base.dao; import com.net.test.redis.base.entity.UserPsg; ...

  3. Python全栈学习:匿名函数使用规范

    匿名函数,当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中,对匿名函数提供了有限支持.还是以map()函数为例,计算f(x)=x2时,除了定义一个f(x) ...

  4. Flask学习笔记:数据库ORM操作MySQL+pymysql/mysql-python+SQLAlchemy/Flask-SQLAlchemy

    Python中使用sqlalchemy插件可以实现ORM(Object Relationship Mapping,模型关系映射)框架,而Flask中的flask-sqlalchemy其实就是在sqla ...

  5. 最小生成树:HDU1863-畅通工程

    畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  6. [USACO]奶牛博览会(DP)

    Description 奶牛想证明他们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N头奶牛进行了面试,确定了每头奶牛的智商和情商. 贝西有权选择让哪些奶牛参加展览.由于负的智商或情商会造成 ...

  7. V4L2学习(四)VIVI分析

    vivi 相对于后面要分析的 usb 摄像头驱动程序,它没有真正的硬件相关层的操作,也就是说抛开了复杂的 usb 层的相关知识,便于理解 V4L2 驱动框架,侧重于驱动和应用的交互. 前面我们提到,V ...

  8. time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  9. 03 Java 虚拟机是如何加载 Java 类的

    Java 引用类型 Java 中的引用类型细分为四种:类,接口,数组类和泛型参数. 因为泛型参数会在编译过程中被擦除,所以 Java 虚拟机实际上只有前三种.数组类是由 Java 虚拟机直接生成的,其 ...

  10. python实现算法: 多边形游戏 数塔问题 0-1背包问题 快速排序

    去年的算法课挂了,本学期要重考,最近要在这方面下点功夫啦! 1.多边形游戏-动态规划 问题描述: 多边形游戏是一个单人玩的游戏,开始时有一个由n个顶点构成的多边形.每个顶点被赋予一个整数值, 每条边被 ...