使用他们的开始时间和方向找到T时间后给定人的位置
有一个长度为N的圆形轨道,给定两个大小为M的数组start[]和direct[]以及一个整数T,其中start[ I ]表示第i个人的起点, direct[ I ]表示顺时针方向如果direct[ I ]为1 ,如果direct[ I ]为-1则逆时针,任务是找到T单位时间后所有人的位置。
Note: Every person moves 1 unit distance in 1 unit of time.
例子:
Input: N = 5, M = 2, T = 1, start[] = {2, 3}, direct[] = {1, -1}
Output: 3 2
Explanation: Given circle has 5 points from 1 to 5 and there are two men let say A and B. A starts from 2nd point and moves clockwise as direct[0] = 1, so after 1 minutes he will be at point 3. Similarly, B starts from 3rd point moving anticlockwise, so after 1 min he will be at point 2. So, ans array contains [3, 2] after sorting it becomes [2, 3].
Input: N = 4, M = 3, T = 2, start[] = {2, 1, 4}, direct[] = {-1, 1, -1}
Output: 4 3 2
方法:解决这个问题的想法是基于对利用圆形轨道的观察。找到T单位时间后的点,取模求答案。请按照以下步骤解决问题:
- 使用变量i遍历范围[0, M)并执行以下任务:
- 将变量t_moves初始化为direct[ I ]*T。
- 将变量end_pos初始化为(((start[i] + t_moves) % N) + N) % N 。
- 如果end_pos为0 ,则将start[ I ]的值设置为N ,否则将其设置为end_pos 。
- 执行上述步骤后,打印数组start[]的值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#include
using namespace std;
// Function to find the final position
// of men's after T minutes
int* positionAfterTMin(int N, int M, int T, int start[],
int direct[])
{
// Find the location of the i-th
// person after T minutes
for (int i = 0; i < M; i++)
{
// Total points moved m by i-th
// person in T minutes
int t_moves = direct[i] * T;
// As the path is circular then
// there is a chance that the
// person will traverse the same
// points again
int end_pos = (((start[i] + t_moves) % N) + N) % N;
// Storing location of the
// i-th person
start[i] = (end_pos == 0) ? N : end_pos;
}
// Returning array which contains
// location of every person moving
// in time T units
return start;
}
// Driver Code
int main()
{
int N = 4;
int M = 3;
int T = 2;
int start[] = { 2, 1, 4 };
int direct[] = { -1, 1, -1 };
int* ans = positionAfterTMin(N, M, T, start, direct);
for (int i = 0; i < M; i++) {
cout << *(ans + i) << " ";
}
return 0;
}
// This code is contributed by Kdheeraj.
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the final position
// of men's after T minutes
public static int[] positionAfterTMin(
int N, int M, int T,
int[] start, int[] direct)
{
// Find the location of the i-th
// person after T minutes
for (int i = 0; i < M; i++) {
// Total points moved m by i-th
// person in T minutes
int t_moves = direct[i] * T;
// As the path is circular then
// there is a chance that the
// person will traverse the same
// points again
int end_pos
= (((start[i] + t_moves)
% N)
+ N)
% N;
// Storing location of the
// i-th person
start[i] = (end_pos == 0) ? N : end_pos;
}
// Returning array which contains
// location of every person moving
// in time T units
return start;
}
// Driver Code
public static void main(String[] args)
{
int N = 4;
int M = 3;
int T = 2;
int[] start = { 2, 1, 4 };
int[] direct = { -1, 1, -1 };
int[] ans = positionAfterTMin(
N, M, T, start, direct);
for (int i = 0; i < ans.length; i++) {
System.out.print(ans[i] + " ");
}
}
}
Python3
# Python program for the above approach
# Function to find the final position
# of men's after T minutes
def positionAfterTMin(N, M, T, start, direct):
# Find the location of the i-th
# person after T minutes
for i in range(M):
# Total points moved m by i-th
# person in T minutes
t_moves = direct[i] * T
# As the path is circular then
# there is a chance that the
# person will traverse the same
# points again
end_pos = (((start[i] + t_moves) % N) + N) % N
# Storing location of the
# i-th person
if end_pos == 0:
start[i] = N
else:
start[i] = end_pos
# Returning array which contains
# location of every person moving
# in time T units
return start
# Driver Code
if __name__ == "__main__":
N = 4
M = 3
T = 2
start = [2, 1, 4]
direct = [-1, 1, -1]
ans = positionAfterTMin(N, M, T, start, direct)
print(ans)
# This code is contributed by Potta Lokesh
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the final position
// of men's after T minutes
public static int[] positionAfterTMin(
int N, int M, int T,
int[] start, int[] direct)
{
// Find the location of the i-th
// person after T minutes
for (int i = 0; i < M; i++) {
// Total points moved m by i-th
// person in T minutes
int t_moves = direct[i] * T;
// As the path is circular then
// there is a chance that the
// person will traverse the same
// points again
int end_pos
= (((start[i] + t_moves)
% N)
+ N)
% N;
// Storing location of the
// i-th person
start[i] = (end_pos == 0) ? N : end_pos;
}
// Returning array which contains
// location of every person moving
// in time T units
return start;
}
// Driver Code
public static void Main(String[] args)
{
int N = 4;
int M = 3;
int T = 2;
int[] start = { 2, 1, 4 };
int[] direct = { -1, 1, -1 };
int[] ans = positionAfterTMin(
N, M, T, start, direct);
for (int i = 0; i < ans.Length; i++) {
Console.Write(ans[i] + " ");
}
}
}
// This code is contributed by shivanisinghss2110
Javascript
// Javascript program for the above approach
// Function to find the final position
// of men's after T minutes
function positionAfterTMin(N, M, T, start, direct)
{
// Find the location of the i-th
// person after T minutes
for (let i = 0; i < M; i++)
{
// Total points moved m by i-th
// person in T minutes
let t_moves = direct[i] * T;
// As the path is circular then
// there is a chance that the
// person will traverse the same
// points again
let end_pos = (((start[i] + t_moves) % N) + N) % N;
// Storing location of the
// i-th person
start[i] = end_pos == 0 ? N : end_pos;
}
// Returning array which contains
// location of every person moving
// in time T units
return start;
}
// Driver Code
let N = 4;
let M = 3;
let T = 2;
let start = [2, 1, 4];
let direct = [-1, 1, -1];
let ans = positionAfterTMin(N, M, T, start, direct);
for (let i = 0; i < 3; i++) {
document.write(ans[i] + " ");
}
// This code is contributed by _saaurabh_jaiswal.
4 3 2
时间复杂度: O(M)
辅助空间: O(1)