题意:给定长为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. POJ 2406 Power String

    算出next数组. 对于任何一个循环字串,len-next[len]必为最小循环节长度 若len%(len-next[len])==0 即为循环字串,n=len/(len-next[len]) 否则输 ...

  2. Linux下重要日志及查看方式

    1.Linux下重要日志文件介绍 /var/log/boot.log 该文件记录了系统在引导过程中发生的事件,就是Linux系统开机自检过程显示的信息,如图1所示: 图1 /var/log/boot. ...

  3. Linux下启动、停止xampp命令

    启动xampp: /opt/lampp/./lampp start 停止xampp: /opt/lampp/./lampp stop 卸载xampp: rm -rf /opt/lampp

  4. [译]The Python Tutorial#10. Brief Tour of the Standard Library

    [译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...

  5. Python9-IO模型-day41

    # 进程:启动多个进程,进程之间是由操作系统负责调用# 线程:启动多个线程,真正由被cpu执行的最小单位实际是线程# 开启一个线程,创建一个线程,寄存器.堆栈# 关闭一个线程# 协程# 本质上是一个线 ...

  6. 单片机入门学习笔记5:STC下载器

    STC下载器主要集成了, 1.芯片识别,下载/编程 2.端口识别 3.串口助手 4.KEIL仿真设置 5.芯片选型 6.范例程序 (集成了定时器,串口等例程) 7.波特率计算器 8.定时器计算器 9. ...

  7. Sublime Text配置python以及快捷键总结

    1.打开Tools > Build System > New Build System.. 2.点击New Build System后,会生成一个空配置文件,在这个配置文件内覆盖配置信息, ...

  8. HDU 3848 CC On The Tree 树形DP

    题意: 给出一棵边带权的树,求距离最近的一对叶子. 分析: 通过DFS计算出\(min(u)\):以\(u\)为根的子树中最近叶子到\(u\)的距离. 然后维护一个前面子树\(v_i\)中叶子到\(u ...

  9. day39---mysql基础三

    1.索引: 字典得目录,便于数据查找. 原理:将列信息存储在其相关的文件,这些信息使用便于检索的方式如B-tree.哈希来存储 索引的分类: 普通所有:name,只能帮助查找 唯一索引:name,帮助 ...

  10. loj2052 「HNOI2016」矿区

    学习一发平面图的姿势--ref #include <algorithm> #include <iostream> #include <cstdio> #includ ...