新品种的大米已经进入超级市场,并且首次面世,这种大米的数量是有限的。每个客户都要求将米装在大小为a和大小为b的两种不同包装中。 a和b的大小由工作人员根据需求确定。
给定小包a和b的大小,可用大米的总量d和顾客数量n,找出给定数量的大米可以满足的最大顾客数量。
显示可以满足的客户总数和可以满足的客户指数。
注意:如果客户订购2 3,则需要2个大小为a的数据包和3个大小为b的数据包。假设客户索引从1开始。
输入:
输入的第一行包含两个整数n和d。下一行包含两个整数a和b。接下来的n行为每个客户包含两个整数,表示客户所需的大小为a和大小为b的袋子总数。
输出:
打印可以满足的最大客户数量,并在下一行中打印满意客户的空格分隔的索引。
例子:
Input : n = 5, d = 5
a = 1, b = 1
2 0
3 2
4 4
10 0
0 1
Output : 2
5 1
Input : n = 6, d = 1000000000
a = 9999, b = 10000
10000 9998
10000 10000
10000 10000
70000 70000
10000 10000
10000 10000
Output : 5
1 2 3 5 6
解释:
在第一个示例中,客户根据其需求的顺序为:
Customer ID Demand
5 1
1 2
2 5
3 8
4 10
由此可以很容易地得出结论,对于总需求1 + 2 = 3,只有客户5和客户1可以满足。其余客户不能购买剩余的大米,因为他们的需求大于可用量。
方法:
为了满足最大数量的顾客的需求,我们必须从需求最小的顾客开始,以便我们有最大的剩余大米以满足剩余顾客的需求。因此,根据需求的增加顺序对客户进行分类,以便可以满足最大数量的客户。
下面是上述方法的实现:
C++
// CPP program to find maximum number
// of customers that can be satisfied
#include
using namespace std;
vector > v;
// print maximum number of satisfied
// customers and their indexes
void solve(int n, int d, int a, int b,
int arr[][2])
{
// Creating an vector of pair of
// total demand and customer number
for (int i = 0; i < n; i++) {
int m = arr[i][0], t = arr[i][1];
v.push_back(make_pair((a * m + b * t),
i + 1));
}
// Sorting the customers according
// to their total demand
sort(v.begin(), v.end());
vector ans;
// Taking the first k customers that
// can be satisfied by total amount d
for (int i = 0; i < n; i++) {
if (v[i].first <= d) {
ans.push_back(v[i].second);
d -= v[i].first;
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << " ";
}
// Driver program
int main()
{
// Initializing variables
int n = 5;
long d = 5;
int a = 1, b = 1;
int arr[][2] = {{2, 0},
{3, 2},
{4, 4},
{10, 0},
{0, 1}};
solve(n, d, a, b, arr);
return 0;
}
Python3
# Python3 program to find maximum number
# of customers that can be satisfied
v = []
# print maximum number of satisfied
# customers and their indexes
def solve(n, d, a, b, arr):
first, second = 0, 1
# Creating an vector of pair of
# total demand and customer number
for i in range(n):
m = arr[i][0]
t = arr[i][1]
v.append([a * m + b * t, i + 1])
# Sorting the customers according
# to their total demand
v.sort()
ans = []
# Taking the first k customers that
# can be satisfied by total amount d
for i in range(n):
if v[i][first] <= d:
ans.append(v[i][second])
d -= v[i][first]
print(len(ans))
for i in range(len(ans)):
print(ans[i], end = " ")
# Driver Code
if __name__ == '__main__':
# Initializing variables
n = 5
d = 5
a = 1
b = 1
arr = [[2, 0], [3, 2],
[4, 4], [10, 0],
[0, 1]]
solve(n, d, a, b, arr)
# This code is contributed by PranchalK
输出:
2
5 1
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。