【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, ...
随机推荐
- mysql切换数据库提示警告:Reading table information for completion of table and column names
登录数据库后,选择数据库时发现以下提示, mysql> use testReading table information for completion of table and column ...
- 【Django】使用list对单个或者多个字段求values值
使用list对values进行求值: 单个字段的输出结果: price_info=list(Book.objects.filter(auth_id='Yu').values('book_price') ...
- 网站动态加载JS脚本
Demo_1 function loadJS(url, fn) { var ss = document.getElementsByName('script'), loaded = false; for ...
- ob缓存的基本使用
在页面 加载的时候 如果 图片 很多 很大 会造成页面的阻塞降低用户体验 我们在点击页面的时候可以使用OB缓存 整个页面, 当用户点击的时候直接请求的是我们预先准备好的html页面 .也降低了我们数据 ...
- UML类图关系模式(C++代码说明)
在UML类图中的关系模式主要有以下几种: 泛化(Generalization), 实现(Realization), 关联(Association), 聚合(Aggregation), 依赖(Depe ...
- A1046 Shortest Distance (20)(20 分)
1046 Shortest Distance (20)(20 分)提问 The task is really simple: given N exits on a highway which form ...
- P2065 贪心的果农
P2065 贪心的果农 题目描述 果农的花园里种着N棵果树.收获的季节终于来到了,果农决定,在接下来的M天时间里完成自己的收获工作.他的收获方式极其暴力——他将会将某棵果树砍倒来获取上面的果实.然而如 ...
- TypeError: cannot perform reduce with flexible type
想要解决这个错误,最好先明白numpy数据类型的dtype转换 生成一个浮点数组 a=np.random.random(4) 输出 a array([0.0945377,0.52199916,0.62 ...
- flex遭遇text-overflow:hidden,white-space:nowrap
最近在项目中遇到使用flex的时候,在flex-item元素中使用text-overflow:hidden:white-space:nowrap:进行省略文字的操作. 发现flex-item失控了,长 ...
- Ecplise实战常用操作快捷键(更新至2018年10月8日 13:46:40)
ctrl+鼠标左键 进入/查看这个类或者方法, ctrl + t 快速类型层次结构(出现部分方法) ctrl + o 快速大 ...