Codeforces 370D - Memory and Trident(模拟)
2016-09-14 18:34
537 查看
B. Memory and Trident
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
Input
The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.
Output
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.
Examples
input
RRU
output
-1
input
UDUR
output
1
input
RUUR
output
2
Note
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.
题意:
给你三角形的边长,前者为大,后者为小.问从大的三角形变到小的三角形需要几步.
解题思路:
逆向思考,从小的变成大的,每次操作都对三边进行排序,让最小的变成剩下两个边的和-1或者大三角形的边长.
直到有一个边的长度达到要求,输出操作次数+2.
AC代码:
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
Input
The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.
Output
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.
Examples
input
RRU
output
-1
input
UDUR
output
1
input
RUUR
output
2
Note
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.
题意:
给你三角形的边长,前者为大,后者为小.问从大的三角形变到小的三角形需要几步.
解题思路:
逆向思考,从小的变成大的,每次操作都对三边进行排序,让最小的变成剩下两个边的和-1或者大三角形的边长.
直到有一个边的长度达到要求,输出操作次数+2.
AC代码:
#include<stdio.h> #include<algorithm> using namespace std; int main() { int a[3]; int n; int m; scanf("%d%d",&n,&m); for(int i = 0;i < 3;i++) a[i] = m; int ans = 0; while(1) { for(int i = 0;i < 3;i++) if(a[i] == n) { printf("%d",ans+2); return 0; } sort(a,a+3); a[0] = min(a[1]+a[2]-1,n); ans++; } }
相关文章推荐
- Codeforces-712B-Memory and Trident
- Codeforces 719C Efim and Strange Grade 贪心+模拟
- Codeforces Round #370 (Div. 2) B. Memory and Trident 水题
- CodeForces 552A-Vanya and Table【模拟】
- Codeforces 832-B Petya and Exam (模拟)
- CodeForces 1208 A. Boys and Girls 模拟序列
- Codeforces--400A--Inna and Choose Options(模拟水题)
- codeforces 442C C. Artem and Array(有深度的模拟)
- Codeforces-754B-Ilya and tic-tac-toe game(简单模拟判断)
- Codeforces-712C-Memory and De-Evolution
- CodeForces 777C Alyona and Spreadsheet【一维数组模拟二维】
- Codeforces--658A--Bear and Reverse Radewoosh(模拟)
- B. Memory and Trident
- Codeforces--400A--Inna and Choose Options(模拟水题)
- CodeForces - 366A Dima and Guards (模拟)
- Codeforces - 277.5 (Div. 2)C - Given Length and Sum of Digits...(模拟 or dfs)
- Codeforces 322C Ciel and Robot【思维+模拟】细节很多= =
- CodeForces 712C - Memory and De-Evolution 逆向思维
- 【Codeforces Round 370 (Div 2) B】【简单贪心】Memory and Trident
- Codeforces 246B-Increase and Decrease【模拟】