给定一个由N 个三元组组成的数组 arr[] ,任务是按降序对三元组进行排序。三重X将大于三重Y具有更高的优先级,当且仅当三元组X的所有元件将大于或等于三元组Y的相应元件打印不可能的,如果三元组不能被排序。
例子:
Input: arr = {{1, 2, 3}, {1, 3, 4}, {4, 7, 4}}
Output: {{4, 7, 4}, {1, 3, 4}, {1, 2, 3}}
Explanation:
As it can be seen, all the corresponding elements of triplet C are greater than or equal to triplet B, and all the corresponding elements of triplet B are greater than or equal to triplet A.
Input: arr = {{1, 2, 3), {1, 2, 4}, {1, 3, 1}, {10, 20, 30}, {16, 9, 25}}
Output: Impossible
方法:这个问题可以使用贪心方法来解决。将所有带有三元组 ID 的三元组保存在不同的列表中,并按降序对这些元组列表进行排序。请按照以下步骤解决问题。
- 创建三个元组列表x , y , 和z 。
- 列表x, y, z将保留带有三元组 ID 的三元组。
- 根据三元组的第一个元素对x进行排序。
- 根据三元组的第二个元素对y进行排序。
- 根据三元组的第 3 个元素对z进行排序。
- 在 [0, N-1]范围内迭代i ,检查所有i ,如果x[i][3] = y[i][3] = z[i][3] ,则打印相应的顺序,否则打印“不可能”。
下面是上述方法的实现。
C++
// C++ program for above appraoch
#include
using namespace std;
// Function to find any possible order
vector> findOrder(vector> &A, vector> &x, vector> &y, vector> &z)
{
int flag = 1;
// Checking if there is any possible ordering
for (int i = 0; i < x.size(); ++i)
{
if (x[i][3] == y[i][3] && y[i][3] == z[i][3])
continue;
else
{
flag = 0;
break;
}
}
vector> Order;
if (flag)
{
for (int i = 0; i < x.size(); ++i)
Order.push_back(A[x[i][3]]);
}
// Return Order
return Order;
}
// Fuction to print order of triplets if
// Any possible
void PrintOrder(vector> &A)
{
// Creating list of paired x, y and z.
vector> x, y, z;
for (int i = 0; i < A.size(); ++i)
{
x.push_back({A[i][0], A[i][1], A[i][2], i});
y.push_back({A[i][1], A[i][0], A[i][2], i});
z.push_back({A[i][2], A[i][0], A[i][1], i});
}
// Sorting of x, y and z
sort(x.rbegin(), x.rend());
sort(y.rbegin(), y.rend());
sort(z.rbegin(), z.rend());
// Function Call
vector> order = findOrder(A, x, y, z);
// Printing Order
if (order.size() == 0)
cout << "Impossible";
else
{
for (auto v : order)
{
for (auto i : v)
cout << i << " ";
cout << "\n";
}
}
}
// Driver Code
int main()
{
vector> A = {{4, 1, 1}, {3, 1, 1}, {2, 1, 1}};
// Function Call
PrintOrder(A);
return 0;
}
// This code is contributed by rakeshsahni
Python3
# Python program for above appraoch
# Function to find any possible order
def findOrder(A, x, y, z):
flag = 1
# Checking if there is any possible ordering
for i in range(len(x)):
if x[i][3] == y[i][3] == z[i][3]:
continue
else:
flag = 0
break
Order = 'Impossible'
if flag:
Order = []
for i, j, k, l in x:
Order += [A[l]]
# Return Order
return Order
# Fuction to print order of triplets if
# Any possible
def PrintOrder(A):
# Creating list of paird x, y and z.
x, y, z = [], [], []
for i in range(len(A)):
x.append((A[i][0], A[i][1], A[i][2], i))
y.append((A[i][1], A[i][0], A[i][2], i))
z.append((A[i][2], A[i][0], A[i][1], i))
# Sorting of x, y and z
x.sort(reverse = True)
y.sort(reverse = True)
z.sort(reverse = True)
# Function Call
order = findOrder(A, x, y, z)
# Printing Order
print(order)
# Driver Code
A = [[4, 1, 1], [3, 1, 1], [2, 1, 1]]
# Function Call
PrintOrder(A)
Javascript
[[4, 1, 1], [3, 1, 1], [2, 1, 1]]
时间复杂度: O(NlogN)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。