CF1073C Vasya and Robot
CF题目难度普遍偏高啊……
一个乱搞的做法。因为代价为最大下标减去最小的下标,那么可以看做一个区间的修改。我们枚举选取的区间的右端点,不难发现满足条件的左端点必然是不降的。那么用一个指针移一下就好了
注意特判无解和答案为\(0\)的情况,时间复杂度\(O(n)\)(然而因为人傻常数大所以还跑不过\(O(nlogn)\)的)
//minamoto
#include<bits/stdc++.h>
#define rint register int
#define GG return puts("-1"),0
using namespace std;
const int N=5e5+5;
struct node{
int x,y;
node(){}
node(int x,int y):x(x),y(y){}
inline node operator +(const int &b)const{
switch(b){
case 0:return node(x-1,y);break;
case 1:return node(x+1,y);break;
case 2:return node(x,y-1);break;
case 3:return node(x,y+1);break;
}
}
inline node operator +(const node &b)const{return node(x+b.x,y+b.y);}
inline node operator -(const node &b)const{return node(x-b.x,y-b.y);}
}sum[N];
char s[N];int val[105],n,ans=0x3f3f3f3f,x,y;
inline int dis(int x,int y,int xx,int yy){return abs(x-xx)+abs(y-yy);}
bool check(int l,int r){
node res=sum[n]-sum[r]+sum[l-1];
return dis(res.x,res.y,x,y)<=r-l+1;
}
int main(){
// freopen("testdata.in","r",stdin);
scanf("%d%s%d%d",&n,s+1,&x,&y);
val['L']=0,val['R']=1,val['D']=2,val['U']=3;
if(dis(0,0,x,y)>n||((dis(0,0,x,y)&1)!=(n&1)))GG;
for(rint i=1;i<=n;++i)sum[i]=sum[i-1]+val[s[i]];
if(sum[n].x==x&&sum[n].y==y)return puts("0"),0;
for(rint i=1,j=1;i<=n;++i){
while(j<i&&check(j+1,i))++j;
if(check(j,i))ans=min(ans,i-j+1);
}printf("%d\n",ans);return 0;
}
CF1073C Vasya and Robot的更多相关文章
- CF 1073C Vasya and Robot(二分答案)
C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 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 ...
- Codeforces 1073C:Vasya and Robot(二分)
C. Vasya and Robot time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard ...
- 【CF1073C】Vasya and Robot(二分,构造)
题意:给定长为n的机器人行走路线,每个字符代表上下左右走,可以更改将一些字符改成另外三个字符,定义花费为更改的下标max-min+1, 问从(0,0)走到(X,Y)的最小花费,无解输出-1 n< ...
- C. Vasya and Robot二分
1.题目描述 Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell ...
- [CF355C]Vasya and Robot(思维,贪心)
题目链接:http://codeforces.com/contest/355/problem/C 题意:1~n n个物品各重wi,现在有一个人可以从左边拿和从右边拿, 左边拿一个物品的花费是l*wi, ...
- cf C. Vasya and Robot
http://codeforces.com/contest/355/problem/C 枚举L和R相交的位置. #include <cstdio> #include <cstring ...
- CodeForce Educational round Div2 C - Vasya and Robot
http://codeforces.com/contest/1073/problem/C 题意:给你长度为n的字符串,每个字符为L, R, U, D.给你终点位置(x, y).你每次出发的起点为( ...
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot
题意:给出一段操作序列 和目的地 问修改(只可以更改 不可以删除或添加)该序列使得最后到达终点时 所进行的修改代价最小是多少 其中代价的定义是 终点序号-起点序号-1 思路:因为代价是终点序号减去 ...
随机推荐
- SQL Server 机考,用T-SQL编写 简单实例
使用T-SQL实现以下要求: 要求如下: 1,添加数据库:MySchool 2,添加学生基础表:Student 3,添加学生成绩表:ScoreInfo 4,两张表结构分别如下 Student表结构:( ...
- intel compiler的表现
好久没弄这个东西,今天突然想试下,代码没写完,以后补. #include <stdio.h> #include <stdlib.h> #include <time.h&g ...
- POJ 3468 线段树区间修改查询(Java,c++实现)
POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...
- spring cloud feign 坑
feign是啥? 很多人可能对于feign 不是很熟悉,可以看一下其他网友的实例分享:spring cloud feign简介 如果觉得上面这个比较难的话,还有一个简单入门的:spring cplou ...
- 【01】JSON基本信息
[魔芋注] 就是一种格式,数据组合的格式. JSON:JavaScript 对象表示法(JavaScript Object Notation).JSON 是存储和交换.传输(数据)文本信息的语法( ...
- Oracle学习总结(4)——MySql、SqlServer、Oracle数据库行转列大全
MySql行转列 以id分组,把name字段的值打印在一行,逗号分隔(默认) select CustomerDrugCode,group_concat(AuditItemName) from noau ...
- JSON.parseObject将json字符串转换为bean类,是否大小写敏感区分---https://blog.csdn.net/mathlpz126/article/details/80684034
JSON.parseObject将json字符串转换为bean类,是否大小写敏感区分 https://blog.csdn.net/mathlpz126/article/details/80684034
- 关于使用freemarker导出word
java使用FreeMarker导出word 一. 先做一个word模板 二. 将该word文件另存为xml格式(注意是另存为,不是直接改扩展名) 三. 打开xml文件把要 ...
- laravel5.5更新到laravel5.7
为什么要更新呢?因为项目用的第三方后台扩展包,有很些bug,不够完美.想要一个漂亮的后台,那个后台只支持5.7. 然后,我就开始更新框架了. 修改后:"php": "&g ...
- [Vue @Component] Place Content in Components with Vue Slots
Vue's slots enable you to define where content of a component should land when you define the conten ...