Title: The number in the array is divided into two groups, giving an algorithm that makes two groups of the sum of the absolute value of the smallest array of values is 0<x<100, the number of elements is greater than 0, less than 100
For example a[]={2,4,5,6,7}, the resulting two groups of {2,4,,6} and {5,7},abs (sum (A1)-sum (a2)) = 0;
For example {2,5,6,10},abs (sum (2,10)-sum (5,6)) = 1, so the two sets of numbers are {2,10} and {5,,6} respectively. Ideas:
At first glance, it feels as if it is a combinatorial problem, and the problem is solved through violent poverty.
But when you think about it, the problem can be converted to find a set of data from the array so that it is as equal to half the array as possible.
Does this look a bit like the 0-1 backpack? Yes, it's a 0-1 knapsack problem .
Condition: The number in the array is the weight value of the knapsack problem, and the number in the array is the value of the knapsack problem, that is, the same.
Question: What items are in the backpack, making it worth as close to half of the total value as possible?
So through the knapsack problem to solve this question is very simple, the following simple statement through the dynamic programming to solve the 0-1 knapsack problem of the idea.
Suppose V[i][j] indicates the maximum value of the item of weight J from the I-item, Weight[i],value[i] represents the weight and value of the article I, respectively (in the title, weight, value belongs to the same array).
The dynamic transfer equation is:
V[I][J]=V[I-1][J] If j<weight[i]
V[i][j]=max (V[i-1][j],v[i-1][j-weight[i]]+value[i]) if j>weight[i]
Also, if you want to know the maximum value that is made up of those items, you can backtrack from backwards, when V[i][j]>v[i-1][j], stating that item I was added (the path is not unique).
Code:
#include <iostream> #include <vector>using namespace std;int knapsack (int num,int c,const vector<int> Weight,const vector<int> value,vector<int> &x); int main () {int w[]={2,4,5,6,7}; int v[]={2,4,5,6,7}; int num=sizeof (W)/sizeof (w[0]); Vector<int> weight (w,w+num); Vector<int> value (v,v+num); int c=12; vector<int> x (num); int Total=knapsack (NUM,C,WEIGHT,VALUE,X); cout<< "Total weight is" <<total<<endl; return 0;} int knapsack (int num,int c,const vector<int> weight,const vector<int> value,vector<int> &x) {Vect or<vector<int> > V (num+1,vector<int> (c+1)); for (int i=1;i<=num;i++) {for (int j=1;j<=c;j++) {if (j<weight[i-1]) v[i][j]=v[i-1] [j]; else V[i][j]=max (v[i-1][j],v[i-1][j-weight[i-1]]+value[i-1]); }} cout<< "Dynamic Matrix:" <<endl; for (int i=1;i<=num;i++) {for (int j=1;j<=c;j++) {cout<<v[i][j]<< ""; } cout<<endl; } int j=c; for (int i=num;i>0;i--) {if (V[i][j]>v[i-1][j]) {x[i]=1; J=J-WEIGHT[I-1]; } else x[i]=0; } cout<< "The articles chosen is:" <<endl; for (int i=0;i<num;i++) {if (X[i]) cout<<i+1<< ""; } cout<<endl; return v[num][c];}
Operation Result:
(written question) divides the array into two groups, minimizing the difference between the two groups