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 思路:因为代价是终点序号减去 ...
随机推荐
- 以gnome-terminal为例,修改gnome3 的默认配置
gnome连续几个版本的terminal默认配置文件都是同一个配置文件“b1dcc9dd-5262-4d8d-a863-c897e6d979b9”.这是因为gnome的developers编辑了这个配 ...
- BZOJ 4919 [Lydsy1706月赛]大根堆 (SRM08 T3)
[题解] 求一个序列的LIS有一个二分做法是这样的:f[i]表示长度为i的上升序列中最后一个数最小可以是多少,每次二分大于等于当前数字x的f[j],把f[j]修改为x:如果找不到这样的f[j],那就把 ...
- 洛谷 2434 [SDOI2005]区间
[题解] 鲜活的大水题... 把区间排个序然后瞎搞就可以了,发现现在区间的左端点比之前区间的最大的右端点还大,那就增加一个答案区间.每次更新目前最大右区间. #include<cstdio> ...
- UVA - 10825 Anagram and Multiplication
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34594 有一个m位n进制的数,它的特性是这个数依次乘以2,3... ...
- DataFrame NaN 替换为零
一个DataFrame 其中有空值NaN,将其替换为0: df.fillna(0) 如果将第一列替换为0: df[1].fillna(0,inplace=True)
- UVa - 12450 - SpaceRecon Tournament
先上题目: Problem G: SpaceRecon Tournament SpaceRecon, the hottest game of 2011, is a real-time strategy ...
- codeforces gym 100357 I (费用流)
题目大意 给出一个或与表达式,每个正变量和反变量最多出现一次,询问是否存在一种方案使得每个或式中有且仅有一个变量的值为1. 解题分析 将每个变量拆成三个点x,y,z. y表示对应的正变量,z表示对应的 ...
- springboot整合dubbo的简单案例
使用框架: jdk 1.8 springboot-2.1.3 dubbo-2.6 spring-data-jpa-2.1.5 一.开发dubbo服务接口: 按照Dubbo官方开发建议,创建一个接口项目 ...
- Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n
错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refre ...
- HDFS v1.0学习笔记
hdfs是一个用于存储大文件的分布式文件系统,是apache下的一个开源项目,使用java实现.它的设计目标是可以运行在廉价的设备上,运行在大多数的系统平台上,高可用,高容错,易于扩展. 适合场景 存 ...