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的更多相关文章

  1. CF 1073C Vasya and Robot(二分答案)

    C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. 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 ...

  3. Codeforces 1073C:Vasya and Robot(二分)

    C. Vasya and Robot time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard ...

  4. 【CF1073C】Vasya and Robot(二分,构造)

    题意:给定长为n的机器人行走路线,每个字符代表上下左右走,可以更改将一些字符改成另外三个字符,定义花费为更改的下标max-min+1, 问从(0,0)走到(X,Y)的最小花费,无解输出-1 n< ...

  5. C. Vasya and Robot二分

    1.题目描述 Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell ...

  6. [CF355C]Vasya and Robot(思维,贪心)

    题目链接:http://codeforces.com/contest/355/problem/C 题意:1~n n个物品各重wi,现在有一个人可以从左边拿和从右边拿, 左边拿一个物品的花费是l*wi, ...

  7. cf C. Vasya and Robot

    http://codeforces.com/contest/355/problem/C 枚举L和R相交的位置. #include <cstdio> #include <cstring ...

  8. CodeForce Educational round Div2 C - Vasya and Robot

    http://codeforces.com/contest/1073/problem/C   题意:给你长度为n的字符串,每个字符为L, R, U, D.给你终点位置(x, y).你每次出发的起点为( ...

  9. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot

    题意:给出一段操作序列 和目的地 问修改(只可以更改 不可以删除或添加)该序列使得最后到达终点时  所进行的修改代价最小是多少 其中代价的定义是  终点序号-起点序号-1 思路:因为代价是终点序号减去 ...

随机推荐

  1. angular5中的自定义指令(属性指令)

    属性型指令用于改变一个 DOM 元素的外观或行为. 在 Angular 中有三种类型的指令: 组件 — 拥有模板的指令 结构型指令 — 通过添加和移除 DOM 元素改变 DOM 布局的指令 属性型指令 ...

  2. 【搜索、bfs】Find The Multiple

    Problem   Given a positive integer n, write a program to find out a nonzero multiple m of n whose de ...

  3. python3.x Day2 购物车程序练习

    购物车程序: 1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 4. ...

  4. response对象处理HTTP文件头(禁用缓存、设置页面自动刷新、定时跳转网页)

    response对象处理HTTP文件头 制作人:全心全意 禁用缓存 在默认情况下,浏览器将会对显示的网页内容进行缓存.这样,当用户再次访问相关网页时,浏览器会判断网页是否有变化,如果没有变化则直接显示 ...

  5. BZOJ 4006 Luogu P3264 [JLOI2015]管道连接 (斯坦纳树、状压DP)

    题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4006 (luogu)https://www.luogu.org/probl ...

  6. excel 2003 默认保存后出现超级连接解决方法

    在excel 2003 中当选中某个单元格然后拷贝出来后发现总是出现超级连接,每次都要取消下很是麻烦 . 于是经过研究找到解决方法,真是累的我够呛 ,先将方法介绍给大家. 工具---自动更正选项--- ...

  7. 【待续】海思Hi3520A学习笔记

    /********************************************************************* * By                       : ...

  8. P1516 青蛙的约会 洛谷

    https://www.luogu.org/problem/show?pid=1516 题目描述 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上, ...

  9. Java使用JNI调用DLL库

    JNI是Java自带的方法,不需要引入第三方jar包,优点是因为是java自带的方法,兼容性较好,缺点就是代码书写繁琐 新建Java项目Test --> 新建测试类TestNative,声明本地 ...

  10. 1. 青蛙跳跳FrogJmp Count minimal number of jumps from position X to Y.

    青蛙跳跳: package com.code; public class Test03_1 { public int solution(int X, int Y, int D) { int res = ...