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 思路:因为代价是终点序号减去 ...
随机推荐
- 关于DEV-c++ 运行窗口闪退的解决办法
因为程序默认运行结束自动关闭,所以运行窗口会被秒关,反复下载了很多遍也没有解决. 上网看过许多博客后,有好多方法,总结一下: ①在return 0:前加getchar():(getchar():是得到 ...
- POJ 3270 置换群问题
题目大意是: 每头牛都有一个对应的值a[i],现在给定一个初始的牛的序列,希望通过两两交换,能够使这些牛按值升序排列,每次交换都会耗费一个 a[i]+a[j] 希望耗费最小,求出这个最小耗费 个人觉得 ...
- [luoguP1196] 银河英雄传说(并查集)
传送门 记录 up[x] 表示 x 上方有多少个 all[x] 表示当前连通的有多少个 find 的时候 和 合并的时候 更新一下即可 ——代码 #include <cstdio> #in ...
- UVA 10692 Huge Mod
Problem X Huge Mod Input: standard input Output: standard output Time Limit: 1 second The operator f ...
- 【BZOJ2081】Beads(哈希表)
题意: 翻转是指其中一段长度为k的子串全部翻转 n<=200000 a[i]<=n 思路:枚举k,直接哈希判充即可 时间复杂度是n/i求和,根据定理可得是O(n log n)级别的 单哈双 ...
- Stealing Harry Potter's Precious BFS+DFS
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
- 【sql技巧】mysql修改时,动态指定要修改的字段 update `table` set (case when ....) = 1 where id = xx
如果你点进了这篇帖子,那么你一定遇到了跟我一样的问题.别看题目的set case when...,我一开始也是第一反应是用case when但是发现并不好使. 问题呢,说得高大上一点:动态指定要修改的 ...
- MyBatis3教程
MyBatis3教程: http://www.yihaomen.com/article/java/302.htm http://www.mybatis.org/mybatis-3/zh/index.h ...
- 23、Java并发性和多线程-重入锁死
以下内容转自http://ifeve.com/reentrance-lockout/: 重入锁死与死锁和嵌套管程锁死非常相似.锁和读写锁两篇文章中都有涉及到重入锁死的问题. 当一个线程重新获取锁,读写 ...
- Lein: Exception in thread "Thread-3" java.net.ConnectException: Connection refused
leiningen Leiningen是你的主要工具, 它用于: 启动一个 REPL 下载+安装类库 运行你的程序 启动一个服务器, 运行你所写的webapps 安装 brew install lei ...