Ultraviolet A-10690 Expression Again
Description
Problem C
Expression Again
Input:Standard input
Output:Standard output
TimeLimit:6 seconds
You are given an algebraic expression of the form(X1 + x2 + x3 +... + xn) * (y1 + y2 +... + ym)And(N + m)Integers. Youhave to find the maximum and minimum value of the expression using the givenintegers. For example if you are given(X1 + x2) * (y1 + y2)And you are given1, 2, 3And4. Then maximum value is(1 + 4) * (2 + 3) = 25Whereas minimum value is(4 + 3) * (2 + 1) = 21.
Input
Each input set starts with two positive integersN, M (less than 51). Next line follows(N + M)Integers which are in therange-50To50. Input is terminated by end of file. Therewill be atmost110Testcases.
Output
Output is one line for each case, maximum valuefollowed by minimum value.
SampleInput Outputfor Sample Input
2 2 1 2 3 4 3 1 1 2 3 4 2 2 2 2 2 2 |
Question: Give You n + m numbers, divide you into n and m, and find the largest and smallest product of their sum.
Idea: Dynamic Planning, set dp [I] [j] to represent the possibility of using I to Form j, and finally solve it from all possibilities
# Include
# Include
# Include
# Include
Using namespace std; const int maxn = 110; int dp [maxn] [maxn * maxn]; int n, m; int main () {while (scanf ("% d", & n, & m )! = EOF) {vector
Ve (n + m + 1); int sum = 0; for (int I = 1; I <= n + m; I ++) {scanf ("% d ", & ve [I]); sum + = ve [I]; ve [I] + = 50;} memset (dp, 0, sizeof (dp )); dp [0] [0] = 1; for (int I = 1; I <= n + m; I ++) {for (int j = min (I, n); j> = 1; j --) // returns the result of for (int k = 0; k <= 10000; k ++) if (dp [J-1] [k]) dp [j] [k + ve [I] = 1;} int Max =-5000; int Min = 5000; for (int I = 0; I <= 10000; I ++) if (dp [n] [I]) {int tmp = I-50 * n; max = max (Max, tmp * (sum-tmp); Min = min (Min, tmp * (sum-tmp ));} printf ("% d \ n", Max, Min);} return 0 ;}