【CF1073C】Vasya and Robot(二分,构造)
题意:给定长为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(二分,构造)的更多相关文章
- C. Vasya and Robot二分
1.题目描述 Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell ...
- Educational Codeforces Round 53 (Rated for Div. 2) C Vasya and Robot 二分
题目:题目链接 思路:对于x方向距离与y方向距离之和大于n的情况是肯定不能到达的,另外,如果n比abs(x) + abs(y)大,那么我们总可以用UD或者LR来抵消多余的大小,所以只要abs(x) + ...
- CF1073C Vasya and Robot
CF题目难度普遍偏高啊-- 一个乱搞的做法.因为代价为最大下标减去最小的下标,那么可以看做一个区间的修改.我们枚举选取的区间的右端点,不难发现满足条件的左端点必然是不降的.那么用一个指针移一下就好了 ...
- 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 ...
- CF 1073C Vasya and Robot(二分答案)
C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces 1073C:Vasya and Robot(二分)
C. Vasya and Robot time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard ...
- 【Atcoder】AGC 020 D - Min Max Repetition 二分+构造
[题意]定义f(A,B)为一个字符串,满足: 1.长度为A+B,含有A个‘A',B个'B'. 2.最长的相同字符子串最短. 3.在满足以上2条的情况下,字典序最小. 例如, f(2,3) = BABA ...
- Codeforces 1073C Vasya and Robot 【二分】
<题目链接> 题目大意: 一个机器人从(0,0)出发,输入一段指令字符串,和机器人需要在指定步数后到达的终点,问如果机器人需要在指定步数内到达终点,那么需要对原指令字符串做出怎样的改变,假 ...
- 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, ...
随机推荐
- C/C++程序基础 (五)位运算
C++中四种转换运算符的区分 const_cast 修改const和volatile属性 reinterpret_cast 指针间类型转换或者指针和整形的转换.二进制重新翻译. static_cast ...
- 一次磁盘IO过高分析过程
1.查看监控,发现整点时间有写IO过高情况
- tcl之基本语法—3
- DeepFaceLab报错,OOM如何解决?
DeepFaceLab出错,虽然错误提示好几个屏幕,但是无非两种情况,一种是驱动没装好,一种是显存配置不够.上一篇文章说了驱动的问题,这一篇就说说配置不够的问题. 这个问题的表现形式,往往是各种OOM ...
- django admin模块使用
BBS之admin组件的使用 1.创建超级管理员 创建超级管理员 一. tools>>>>runmanagepyTask>>>>>createsu ...
- mysql-不恰当的update语句使用主键和索引导致mysql死锁
背景知识:MySQL有三种锁的级别:页级.表级.行级. MyISAM和MEMORY存储引擎采用的是表级锁(table-level locking):BDB存储引擎采用的是页面锁(page-level ...
- Diycode开源项目 MainActivity分析
1.分析MainActivity整体结构 1.1.首先看一下这个界面的整体效果. 1.2.活动源代码如下 /* * Copyright 2017 GcsSloop * * Licensed under ...
- iview框架 两侧弹框 出现第二层弹框 一闪而过的问题
分析原因:寡人怀疑可能是,两层弹出框 采用的是一个开关值,发生了覆盖 解决方式 是在第二层弹框外套层计时器 源代码如下: 修改后为:
- RxJava Rxandroid retrofit
其实Retrofit会了.集合RxJava,RxAndroid 就很简单了. 只需要改几个地方. 1.接口里面返回的对象不再是 call,而是Observable public interface A ...
- Redis实现之数据库(二)
设置键的生存时间或过期时间 通过EXPIRE或PEXPIRE命令,客户端可以以秒或者毫秒精度为数据库中的某个键设置生存时间(Time To Live,TTL),在经过指定的秒数或者毫秒数之后,服务器就 ...