为了使一个集合的总和大于另一个集合而需要选择的最小数组索引数
给定两个大小为N的数组arr[]和brr[]和一个整数K 。考虑两个集合A ,最初包含K和B ,最初是空的。在每个操作中,都需要选择一个索引。对于每个选定的索引,比如i , arr[i]和brr[i]被添加到B 。对于每个未选择的索引,将arr[i]添加到A。任务是找到使B的总和大于A 的总和所需选择的最小索引数。如果不能这样做,则打印 -1。
例子:
Input: arr[] = {3, 2, 5, 6}, brr[] = {4, 4, 2, 3}, K = 12
Output: 3
4 3 1
Explanation: Indices 2, 3 and 0 are selected. Sum of B = arr[0] + brr[0] + arr[2] + brr[2] + arr[3] + brr[3] = 3 + 4 + 5 + 2 + 6 + 3 = 23. Sum of A = K + arr[1] = 12 + 2 = 14.
Input: arr[] = {3, 2, 5, 6}, brr[] = {4, 4, 2, 3}, K = 34
Output: -1
方法:这个想法是使用贪婪方法。请按照以下步骤解决问题:
- 初始化对向量B[]以跟踪索引。
- 用K初始化变量A以存储集合A的值。
- 使用变量i遍历数组arr[] ,
- 如果未选择索引,则将值arr[i]添加到A 。
- 将 {brr[i] + 2 * arr[i], i}作为一对插入向量B中。
- 以降序对向量B进行排序。
- 初始化一个向量, ans来存储选择的索引。
- 运行一个 while 循环并继续选择索引,直到A 的值大于B 。
- 如果选择了所有索引,但B 的值仍然小于A ,则打印“-1” 。
- 否则,将向量ans的大小打印为最小移动次数。
- 遍历向量ans并打印选择的索引。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the minimum number
// of indices required to be selected
void numberOfIndexes(int arr[], int brr[],
int N, int K)
{
// Declare vector to keep track of indexes
vector > B;
// Set A contains K
int A = K;
// Traverse the array
for (int i = 0; i < N; i++) {
// Adding value that set A can
// get if no index was chosen
A += arr[i];
// Insert as B's value
B.push_back({ brr[i] + 2 * arr[i], i });
}
// Sort the vector
sort(B.begin(), B.end());
// Reverse the vector
reverse(B.begin(), B.end());
int tot = 0, idx = 0;
// Stores chosen indexes
vector ans;
// Keep on choosing more indices until
// B's value is bigger than A or stop
// incase all the indexes is chosen
while (A >= tot && idx < B.size()) {
// Update tot
tot += B[idx].first;
// Update ans
ans.push_back(B[idx].second);
// Increment idx
idx++;
}
// If all indices are selected and
// sum of B is less than sum of A
if (tot <= A) {
cout << -1 << endl;
return;
}
// Print the minimum number of indices
cout << ans.size() << endl;
// Print chosen indices
for (auto c : ans)
cout << c + 1 << " ";
}
// Driver Code
int main()
{
// Given arrays
int arr[] = { 3, 2, 5, 6 };
int brr[] = { 4, 4, 2, 3 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Given value of K
int K = 12;
// Function Call
numberOfIndexes(arr, brr, N, K);
return 0;
}
Python3
# Python 3 program for the above approach
# Function to print the minimum number
# of indices required to be selected
def numberOfIndexes(arr, brr, N, K):
# Declare vector to keep track of indexes
B = []
# Set A contains K
A = K
# Traverse the array
for i in range(N):
# Adding value that set A can
# get if no index was chosen
A += arr[i]
# Insert as B's value
B.append([brr[i] + 2 * arr[i], i])
# Sort the vector
B.sort()
# Reverse the vector
B = B[::-1]
tot = 0
idx = 0
# Stores chosen indexes
ans = []
# Keep on choosing more indices until
# B's value is bigger than A or stop
# incase all the indexes is chosen
while (A >= tot and idx < len(B)):
# Update tot
tot += B[idx][0]
# Update ans
ans.append(B[idx][1])
# Increment idx
idx += 1
# If all indices are selected and
# sum of B is less than sum of A
if (tot <= A):
print(-1)
return
# Print the minimum number of indices
print(len(ans))
# Print chosen indices
for c in ans:
print(c + 1,end = " ")
# Driver Code
if __name__ == '__main__':
# Given arrays
arr = [3, 2, 5, 6]
brr = [4, 4, 2, 3]
# Size of the array
N = len(arr)
# Given value of K
K = 12
# Function Call
numberOfIndexes(arr, brr, N, K)
# This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
3
4 3 1
时间复杂度: O(N*log(N))
辅助空间: O(N)