CodeForces - 1117C

You a captain of a ship. Initially you are standing in a point (x1,y1)(x1,y1) (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point (x2,y2)(x2,y2).

You know the weather forecast — the string ss of length nn, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side s1s1, the second day — s2s2, the nn-th day — snsn and (n+1)(n+1)-th day — s1s1 again and so on.

Ship coordinates change the following way:

  • if wind blows the direction U, then the ship moves from (x,y)(x,y)to (x,y+1)(x,y+1);
  • if wind blows the direction D, then the ship moves from (x,y)(x,y)to (x,y−1)(x,y−1);
  • if wind blows the direction L, then the ship moves from (x,y)(x,y)to (x−1,y)(x−1,y);
  • if wind blows the direction R, then the ship moves from (x,y)(x,y)to (x+1,y)(x+1,y).

The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction U and the ship moves the direction L, then from point (x,y)(x,y) it will move to the point (x−1,y+1)(x−1,y+1), and if it goes the direction U, then it will move to the point (x,y+2)(x,y+2).

You task is to determine the minimal number of days required for the ship to reach the point (x2,y2)(x2,y2).

Input

The first line contains two integers x1,y1x1,y1 (0≤x1,y1≤1090≤x1,y1≤109) — the initial coordinates of the ship.

The second line contains two integers x2,y2x2,y2 (0≤x2,y2≤1090≤x2,y2≤109) — the coordinates of the destination point.

It is guaranteed that the initial coordinates and destination point coordinates are different.

The third line contains a single integer nn (1≤n≤1051≤n≤105) — the length of the string ss.

The fourth line contains the string ss itself, consisting only of letters U, D, L and R.

Output

The only line should contain the minimal number of days required for the ship to reach the point (x2,y2)(x2,y2).

If it's impossible then print "-1".

Examples

Input

0 0
4 6
3
UUU

Output

5

Input

0 3
0 0
3
UDD

Output

3

Input

0 0
0 1
1
L

Output

-1

In the first example the ship should perform the following sequence of moves: "RRRRU". Then its coordinates will change accordingly: (0,0)(0,0) →→ (1,1)(1,1) →→ (2,2)(2,2) →→ (3,3)(3,3) →→ (4,4)(4,4) →→ (4,6)(4,6).

In the second example the ship should perform the following sequence of moves: "DD" (the third day it should stay in place). Then its coordinates will change accordingly: (0,3)(0,3) →→ (0,3)(0,3) →→ (0,1)(0,1) →→ (0,0)(0,0).

In the third example the ship can never reach the point (0,1)(0,1).

思路很简单:二分天数,然后check下是否可以。可以向下二分,不可以向上二分。

天数上界调大点就行了。

 

不过有一点求大佬告知!!就是我注释的代码和 下面的for循环操作  性质上是一样的,但结果WA在了8.。

就下面这组数据,不知道为啥,是编译原理的问题吗????求解啊啊啊!!!

 

更新:  发现问题了,注释下的代码没更新sx[i],sy[i]数组,(只更新了一个方向上的,这竟然神奇的过了7个数据);

	#include<cstdio>
	#include<iostream>
	#include<algorithm>
	#define maxn 100000+100
	typedef long long ll;
	using namespace std;
	char s[maxn];
	ll sumx[maxn],sumy[maxn];
	ll las(ll a)
	{
		if(a>0)
		return a;
		return -a;
	}
	int main()
	{
		ll x1,x2,y1,y2,len;
		cin>>x1>>y1>>x2>>y2>>len;
		cin>>s;
		for(int i=1;i<=len;++i)
		{
			sumx[i]=sumx[i-1];
			sumy[i]=sumy[i-1];
			if(s[i-1]=='U')
				++sumy[i];
			else
				if(s[i-1]=='D')
					--sumy[i];
				else
					if(s[i-1]=='L')
						--sumx[i];
					else
						++sumx[i];
		}
	/*	sumx[0]=0;
		sumy[0]=0;
		ll sx=0,sy=0 ;
		for(int i=1;i<=len;++i)
		{
			
			if(s[i-1]=='U')
				sumy[i]=++sy;
			else
				if(s[i-1]=='D')
					sumy[i]=--sy;
				else
					if(s[i-1]=='L')
						sumx[i]=--sx;
					else
						sumx[i]=++sx;
		}*/ 
	//	printf("%lld--%lld\n",sx[len],sy[len]);
		ll l=1,r=1e18,nn,ans=-1,nx,ny;
		while(l<=r)
		{
			ll mid=(l+r)/2;//mid为二分出的天数 
			nx=x1+mid/len*sumx[len]+sumx[mid%len];
			ny=y1+mid/len*sumy[len]+sumy[mid%len];
			nn=las(x2-nx)+las(y2-ny);
			if(mid>=nn)
			{
				r=mid-1;
				ans=mid;
			}
			else
			{
				l=mid+1;
			}
		}
		cout<<ans<<endl;
		return 0;
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值