列表

详情


NC222157. CocktailWithPony

描述

There is a cute pony! Oh No, why a wolf chasing it behind?

In a one-dimensional number axis, the wolf initial position is point x_1, and the pony at point x_2 initially. Each round, the wolf must move v_1 steps, the pony must move v_2 steps. Each step can move one point to the left or right.
Now the pony and the wolf take turns to move. The pony moves first in the first round, then the wolf moves in the second round, and so on.
Means, the pony moves in the rounds, and the wolf moves in the rounds.
If at a certain moment, the wolf and the pony stand in the same position, it means that the wolf has caught the pony. This moment can be in the process of moving.
It is guaranteed that at the beginning, the little pony and the wolf are not in the same point.

The pony wants to delay the rounds he be caught as many as possible, while the wolf hopes to catch the pony earlier. If both the wolf and the little pony are smart enough, in the first few rounds, will the wolf catch the little pony?

Note: During the movement of the wolf or the little horse, they cannot exceed the boundary of the number axis, that is, any position  they move to must satisfy

输入描述

The first line of input contains a positive integer , which represents the number of test cases.

Each test contains five positive integers and guarantee .The meaning is as shown in the statement.

输出描述

For each test data, output a positive integer in one line to indicate that the wolf needs several rounds to catch the pony. 

示例1

输入:

1
10 3 1 4 8

输出:

4

说明:

The pony moves to position {9} in the first round, and the wolf moves to position {7} in the second round. In the third round, the little pony must move, so it can only move to position {8} or {10}. No matter which position it is, the wolf can catch up in the fourth round. So output {4}.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 74ms, 内存消耗: 796K, 提交时间: 2021-05-26 00:12:30

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	int n,v1,v2,x1,x2;
	while(t--)
	{
		cin>>n>>v1>>v2>>x1>>x2;
		if(x1<x2)
		{
			x1=n-x1+1;
			x2=n-x2+1;
		}
		int cnt=1;
		while(true)
		{
			if(cnt&1)
			{
				if(x2==1&&x1==2)
				break;
				if(x2-v2>=1)
				x2-=v2;
				else if((v2-x2)%2==0)
				{
					x2=2;
				}
				else
				x2=1;
			}
			else
			{
				if(x1-x2<=v1)
				break;
				x1-=v1;
			}
			cnt++;
		}
		cout<<cnt<<endl;
	}
}

上一题