假設有一個製造商為特定產品製造特定零件。製造商有n種不同的零件變體,這些零件在三個標準上有特定的評級。 n個產品的評級在數組'ratings'中給出,其中每個元素的格式為(A, B, C),其中A、B和C是產品的不同評級標準。現在,一個OEM想要從零件製造商購買每種產品所需的m個零件。 OEM選擇符合以下條件的零件:
不能購買兩個或更多相同的零件。
選擇一組零件,使數值V最大化,其中V = |標準A的總評級| |標準B的總評級| |標準C的總評級|。
我們要找出OEM選擇的零件中V的最大可能值。
所以,若輸入是n = 6,m = 4,ratings = {{2, 3, 5}, {3, 5, 2}, {4, 8, 5}, {1, 5 , 3}, {7, 2, 7}, {4, 3, 6}},則輸出將是56。
如果OEM選擇零件1、3、5和6,那麼每個類別的總評級為:
Category A = 2 + 4 + 7 + 4 = 17 Category B = 3 + 8 + 2 + 3 = 16. Category C = 5 + 5 + 7 + 6 = 23 The total value of V is 17 + 16 + 23 = 56.
為了解決這個問題,我們將按照以下步驟進行:
N := 100 Define an array arr of size: 9 x N. Define an array ans. for initialize i := 0, when i < n, update (increase i by 1), do: a := first value of ratings[i] b := second value of ratings[i] c := third value of ratings[i] arr[1, i] := a + b + c arr[2, i] := a - b - c arr[3, i] := a + b - c arr[4, i] := a - b + c arr[5, i] := -a + b + c arr[6, i] := -a - b - c arr[7, i] := -a + b - c arr[8, i] := -a - b + c for initialize i := 1, when i <= 8, update (increase i by 1), do: sort the array arr[i] for initialize i := 1, when i <= 8, update (increase i by 1), do: reverse the array arr[i] if m is the same as 0, then: V := 0 Otherwise for initialize j := 1, when j <= 8, update (increase j by 1), do: k := 0 for initialize i := 0, when i < m, update (increase i by 1), do: k := k + arr[j, i] V := maximum of V and k return V
讓我們看下面的實作以便更好地理解−
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int modval = (int) 1e9 + 7; #define N 100 int solve(int n, int m, vector<tuple<int, int, int>> ratings) { int V, arr[9][N] ; vector<int> ans ; for(int i = 0 ; i < n ; i++) { int a, b, c; tie(a, b, c) = ratings[i]; arr[1][i] = a + b + c ; arr[2][i] = a - b - c ; arr[3][i] = a + b - c ; arr[4][i] = a - b + c ; arr[5][i] = -a + b + c ; arr[6][i] = -a - b - c ; arr[7][i] = -a + b - c ; arr[8][i] = -a - b + c ; } for(int i = 1 ; i <= 8 ; i++) sort(arr[i] , arr[i] + n) ; for(int i = 1 ; i <= 8 ; i++) reverse(arr[i] , arr[i] + n) ; if (m == 0) V = 0 ; else { for (int j = 1; j <= 8; j++) { int k = 0; for (int i = 0; i < m; i++) k += arr[j][i]; V = max(V, k); } } return V; } int main() { int n = 6, m = 4; vector<tuple<int, int, int>> ratings = {{2, 3, 5}, {3, 5, 2}, {4, 8, 5}, {1, 5, 3}, {7, 2, 7}, {4, 3, 6}}; cout<< solve(n, m, ratings); return 0; }
6, 4, {{2, 3, 5}, {3, 5, 2}, {4, 8, 5}, {1, 5, 3}, {7, 2, 7}, {4, 3,6}}
56
以上是C++ 程式找出最大評級零件集合的詳細內容。更多資訊請關注PHP中文網其他相關文章!