Codeforces Round #370 (Div. 2)B. Memory and Trident
地址:http://codeforces.com/problemset/problem/712/B
题目:
2 seconds
256 megabytes
standard input
standard output
Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion:
- An 'L' indicates he should move one unit left.
- An 'R' indicates he should move one unit right.
- A 'U' indicates he should move one unit up.
- A 'D' indicates he should move one unit down.
But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.
The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.
If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.
RRU
-1
UDUR
1
RUUR
2
In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.
In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to "LDUR". This string uses 1 edit, which is the minimum possible. It also ends at the origin.
思路: x=abs(d[1]-d[3]);y=abs(d[2]-d[4]);
ans+=min(y,x)+abs(y-x)/2;
代码:
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-;
const double pi = acos(-1.0);
const int K = 1e6 + ; char ss[K+];
int d[]; int main(void)
{
int len,x,y,ans=;
cin>>ss+;
len=strlen(ss+);
if(len&)
{
printf("-1\n");
return ;
}
for(int i=;i<=len;i++)
if(ss[i]=='U')
d[]++;
else if(ss[i]=='R')
d[]++;
else if(ss[i]=='D')
d[]++;
else if(ss[i]=='L')
d[]++;
x=abs(d[]-d[]);
y=abs(d[]-d[]);
ans+=min(y,x)+abs(y-x)/;
cout<<ans<<endl;
return ;
}
Codeforces Round #370 (Div. 2)B. Memory and Trident的更多相关文章
- Codeforces Round #370 (Div. 2) B. Memory and Trident 水题
B. Memory and Trident 题目连接: http://codeforces.com/contest/712/problem/B Description Memory is perfor ...
- Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树
E. Memory and Casinos 题目连接: http://codeforces.com/contest/712/problem/E Description There are n casi ...
- Codeforces Round #370 (Div. 2)C. Memory and De-Evolution 贪心
地址:http://codeforces.com/problemset/problem/712/C 题目: C. Memory and De-Evolution time limit per test ...
- Codeforces Round #370 (Div. 2) D. Memory and Scores 动态规划
D. Memory and Scores 题目连接: http://codeforces.com/contest/712/problem/D Description Memory and his fr ...
- Codeforces Round #370 (Div. 2) C. Memory and De-Evolution 水题
C. Memory and De-Evolution 题目连接: http://codeforces.com/contest/712/problem/C Description Memory is n ...
- Codeforces Round #370 (Div. 2) A. Memory and Crow 水题
A. Memory and Crow 题目连接: http://codeforces.com/contest/712/problem/A Description There are n integer ...
- Codeforces Round #370 (Div. 2) E. Memory and Casinos (数学&&概率&&线段树)
题目链接: http://codeforces.com/contest/712/problem/E 题目大意: 一条直线上有n格,在第i格有pi的可能性向右走一格,1-pi的可能性向左走一格,有2中操 ...
- Codeforces Round #370 (Div. 2) D. Memory and Scores DP
D. Memory and Scores Memory and his friend Lexa are competing to get higher score in one popular c ...
- Codeforces Round #370 (Div. 2) A B C 水 模拟 贪心
A. Memory and Crow time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- js限制input输入
1.取消按钮按下时的虚线框,在input里添加属性值 hideFocus 或者 HideFocus=true <input type="submit" value=" ...
- [python学习笔记]Day3
函数 如: def is_leapyear(year): if (year%4 == 0 and year%100 != 0) or (year%400 == 0): return True else ...
- ILNumerics项目的应用之线性方程
ILNumerics是一个开源的数值项目,一种NET框架的高性能数学库,它简化了各种数学算法的使用,优化到了C和FORTRAN的速度.现在它提供了一个支持"任何CPU" ...
- Engine中如何更改矢量图层字段别名?
[解决办法]:使用IClassSchemaEdit.AlterFieldAliasName方法可以更改数据源的别名,如果想在图层的属性中更改显示的别名需要使用ITableFields.FieldInf ...
- SPServices.SPDisplayRelatedInfo
Function $().SPServices.SPDisplayRelatedInfo Certification Functionality SPDisplayRelatedInfo is a f ...
- 【读书笔记】iOS-验证应用内支付的凭证注意事项
1,简单来说,越狱后的手机由于没有沙盒作为保护,黑客可以对系统进行任意的修改,所以,在支付过程中,苹果返回的已付款成功的凭证可能是伪造的.客户端拿到付款凭证之后,还需要将凭证上传到自己的服务器,进行二 ...
- JSON取值前判断
public static void main(String[] args)throws Exception{ String jsonStr1="{\"access_token\& ...
- Mac terminal从bash切换到zsh
0.预备知识 echo $SHELL命令可以查看当前正在使用什么shell 默认情况下(mbp 10.10.5)使用bash作为默认shell,然而也自带zsh,which zsh命令可以查看zsh的 ...
- nginx配置入门
谢谢作者的分享精神,原文地址:http://www.nginx.cn/591.html nginx配置入门 之前的nginx配置是对nginx配置文件的具体含义进行讲解,不过对于nginx的新手可能一 ...
- rabbitmq-server启动不了,安装erlang,安装rabbitmq-server
sudo rabbitmq-server start,虽然现实success,但是查看状态,sudo rabbitmq-server status发现居然没有启动,报错是不是端口占用(查看日志/var ...