CF 752C. Santa Claus and Robot

时间:2024-01-05 12:14:56

C. Santa Claus and Robot

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.

While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.

Input

The first line of input contains the only positive integer n (1 ≤ n ≤ 2·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R — to the right, U — to the top and D — to the bottom. Have a look at the illustrations for better explanation.

Output

The only line of input should contain the minimum possible length of the sequence.

Examples
input
4
RURD
output
2
input
6
RRULDD
output
2
input
26
RRRULURURUULULLLDLDDRDRDLD
output
7
input
3
RLL
output
2
input
4
LRLR
output
4
Note

The illustrations to the first three tests are given below.

CF 752C. Santa Claus and RobotCF 752C. Santa Claus and RobotCF 752C. Santa Claus and Robot

The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence.

倒着走一遍,边走边判断,要不走包围当前两点路线的矩形的长与宽之和,要不走当前路,打擂台比小的值,判断是否放礼物。水~

#include<iostream>
#include<string>
#include<cmath>
using namespace std; int n,x,y,lx,ly,ans=1,cnt;
string s; int main()
{
cin>>n>>s;
for(int i=0;i<n;i++)
{
if(s[i]=='R')s[i]='L';
else if(s[i]=='L')s[i]='R';
else if(s[i]=='U')s[i]='D';
else s[i]='U';
} for(int i=n-1;i>=0;i--)
{
cnt++;
int px=x,py=y;
if(s[i]=='L')x--;
else if(s[i]=='R')x++;
else if(s[i]=='D')y--;
else y++;
//cout<<x<<" "<<y<<endl;
if(abs(lx-x)+abs(ly-y)<cnt)
{
ans++;
cnt=1;
lx=px;
ly=py;
//cout<<px<<" "<<py<<endl;
}
}
cout<<ans<<endl;
return 0;
}