【二分答案】【數學思維】codeforces1117C Magic Ship

C. Magic Ship
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You a captain of a ship. Initially you are standing in a point (x1,y1) (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point (x2,y2).

You know the weather forecast — the string s of length n, 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 s1, the second day — s2, the n-th day — sn and (n+1)-th day — s1 again and so on.

Ship coordinates change the following way:

if wind blows the direction U, then the ship moves from (x,y) to (x,y+1);
if wind blows the direction D, then the ship moves from (x,y) to (x,y−1);
if wind blows the direction L, then the ship moves from (x,y) to (x−1,y);
if wind blows the direction R, then the ship moves from (x,y) to (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) it will move to the point (x−1,y+1), and if it goes the direction U, then it will move to the point (x,y+2).

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

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

The second line contains two integers x2,y2 (0≤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 n (1≤n≤105) — the length of the string s.

The fourth line contains the string s 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).

If it’s impossible then print “-1”.


 很好的一道題,題意很簡明。比賽時心裏一直在想是不是二分答案,開始感覺不想然後有往其它地方想,後來仔細感受了一下,似乎確實是滿足二分性質的,於是當時匆匆寫了下。
 實際上,可以發現,四個方向的前進對於船和目的地的相對曼哈頓距離只有兩個貢獻,增加1或者減少1。由於船只可以選擇停止,所以,要到達目的地的話,船行進的時間是小於或等於承受風的時間的,絕對不能大於。將風的作用和船的行進分開來看待,首先風颳過時間T,使得相對曼哈頓距離爲S,然後船在T時間最多使得距離縮小T,所以T>=S。
 繪製圖像, 橫軸爲時間,縱軸爲距離,第一條是直線y=T,第二條是y=S,這是一條起點爲非負數,斜率的絕對值爲1的折線。很容易發現如果兩者在一處相交,必然在此之後都有T>=S滿足,所以,如果在時間T船可以到達目的地,那麼在之後的任何時間都可以到目的地。於是可以做二分答案。
 這種思路也可以限定出答案的範圍,起始距離最多爲2E9,如果到達,每n步至少要靠近一次,也就是距離縮短2,由於n<=1E5,所以可以求出二分的上限是1E14。(媽呀其實當時寫二分的時候哪裏想了這麼多,湊巧就寫了個1E14,再小一點就要被hack掉了QwQ)

#include<cstdio>
#include<algorithm>
using namespace std;
using LL=long long;

LL L,R=1E14,x,y,x1[100005],y1[100005];
int n;
char s[100005];

bool check(LL m)
{
	LL p=m/n*x1[n],q=m/n*y1[n];
	p+=x1[m%n],q+=y1[m%n];
	return abs(x-p)+abs(y-q)<=m;
}

int main()
{
	scanf("%lld%lld%lld%lld%d%s",&x1[0],&y1[0],&x,&y,&n,s+1);
	x-=x1[0],y-=y1[0];
	x1[0]=y1[0]=0;
	for(int i=1;i<=n;i++)
	{
		x1[i]=x1[i-1],y1[i]=y1[i-1];
		if(s[i]=='U')
			y1[i]++;
		else if(s[i]=='D')
			y1[i]--;
		else if(s[i]=='L')
			x1[i]--;
		else
			x1[i]++;
	}
	while(L<R)
	{
		LL M=L+R>>1;
		if(check(M))
			R=M;
		else
			L=M+1;
	}
	if(!check(L))
		printf("-1");
	else
		printf("%lld",L);
	return 0;
}

 實際上如果你想到了作圖這一步,不用二分答案也能求了。因爲兩個曲線的縱座標之差是一個週期變化一個定值,所以直接做一個除法是可以求出相交是在哪個週期中,接着枚舉具體是在哪一步。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章