【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, ...
随机推荐
- js控制时间显示格式
Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.g ...
- MySQL基础 - 1 数据库基础
一.数据库基础 1.什么是数据库 1.数据库(database)是保存有组织的数据的容器( 通常是一个文件或一组文件 ) 2.数据库是一个以某种有组织的方式存储的数据集合 注意:数据库软件应该称为DB ...
- linux正则表达式企业级深度实践案例2
[root@redhat~]# sed -nr ' s#([ ^ : ]+) (: .* :) (/.*$)#\3\2\1#gp ' /etc/passwd
- Ajax基础知识梳理
Ajax用一句话来说就是无须刷新页面即可从服务器取得数据.注意,虽然Ajax翻译过来叫异步JavaScript与XML,但是获得的数据不一定是XML数据,现在服务器端返回的都是JSON格式的文件. 完 ...
- java中的访问修饰符 (2013-10-11-163 写的日志迁移
访问级别 修饰符 同类 同包 子类 ...
- Mybaitis 与jdbc
jdbc读取数据库从resultSet中遍历结果集,存在硬编码(写死的),不利于系统维护,所以最好能将结果集自动映射成java对象 由此产生了mybatis.
- 谭浩强C第四版(p141)16.输出以下图案
运行结果: * *** ***** ******* ***** *** * Press any key to continue #include<stdio.h> int main() { ...
- n个人排队都不站在原来的位置
一.题目描述 有n个人首先站成一排,请问,当n个人第二次再重新排列,每个人都不在原来的位置上,问有多少种站法.例如,原来有3个人,ABC,那么第二次每个人都不在原来的位置上有2种站法,BCA和CAB, ...
- 小米r3g旧版开发版固件,安装opkg
1.开启ssh 1.1.刷入固件 在路由器更新界面,刷入 miwifi_r3g_firmware_c2175_2.25.122.bin 固件 下载地址: http://bigota.miwifi.co ...
- 连续小波变换CWT(2)
如果让你说说连续小波变换最大的特点是什么?多分辨分析肯定是标准答案.所谓多分辨分析即是指小波在不同频率段会有不同的分辨率.具体表现形式,我们回到前一篇文章的第一个图, 图一 对应的信号为 低频时(频率 ...