Question: There is an unordered positive integer group with a 2n number of elements. Required: How can I divide this array into two arrays with N numbers and make the two subarrays closer to the nearest one?
1 1 2-> 1 1 vs 2
Generally, the time complexity of the solution is greater than or equal to O (N ^ 2 ). A sudden burst of inspiration, and a new solution, should be considered a dynamic planning process, the idea is relatively simple, please refer to the code. Space complexity O (nlogn) and time complexity O (n ). However, you are not sure whether all positive integer groups are applicable. If there are any errors, Please provide your test case. Thank you!
The Code is as follows:
1 1 # include <iostream> 2 2 # include <vector> 3 3 using namespace STD; 4 4 5 5 Int compare (const void * in_ileft, const void * in_iright) {6 6 int * A = (int *) in_ileft; 7 7 int * B = (int *) in_iright; 8 8 return * A-* B; 9 9 10 10} 11 11 int main () {12 vector <int> t_vec; 13 13 int t_ivalue; 14 14 int t_ilen = 0; 15 15 CIN> t_ivalue; 16 16 while (t_ivalue! = 0) {// If the input is 0, stop 17 t_vec.push_back (t_ivalue); 18 18 t_ilen ++; 19 19 CIN> t_ivalue; 20 20 21 21} 22 22 int * t_parr = new int [t_ilen]; 23 23 for (INT I = 0; I <t_ilen; I ++) {24 24 t_parr [I] = t_vec [I]; 25 25} 26 qsort (t_parr, t_ilen, sizeof (INT), compare ); 27 27 int * t_pleftarr = new int [t_ilen]; 28 int * t_prightarr = new int [t_ilen]; 29 29 memset (t_pleftarr, 0, t_ilen); 30 30 memset (t_prighta RR, 0, t_ilen); 31 31 int t_isumleft = t_parr [t_iLen-1]; 32 32 int t_isumright = t_parr [t_iLen-2]; 33 33 int t_ileftiter = 1; 34 34 int t_irightiter = 1; 35 35 t_pleftarr [0] = t_parr [t_iLen-1]; 36 36 t_prightarr [0] = t_parr [t_iLen-2]; 37 37 for (INT I = t_ilen-3; I> = 0; I --) {38 38 If (t_isumleft <t_isumright) {39 39 if (t_parr [I]> 0) {40 40 t_pleftarr [t_ileftiter ++] = t_parr [I]; 41 41 t_isumleft + = T_parr [I]; 42 42} else {43 t_prightarr [t_irightiter ++] = t_parr [I]; 44 t_isumright + = t_parr [I]; 45 45} 46 46} else {47 47 If (t_parr [I]> 0) {48 48 t_prightarr [t_irightiter ++] = t_parr [I]; 49 49 t_isumright + = t_parr [I]; 50 50} else {51 t_pleftarr [t_ileftiter ++] = t_parr [I]; 52 52 t_isumleft + = t_parr [I]; 53 53} 54 54} 55 55} 56 56 cout <"print original sequence:" <Endl; 57 57 for (INT I = 0; I <t_ilen; I ++) {58 58 If (t_parr [I]! = 0) {59 59 cout <t_parr [I] <"; 60 60} else {61 61 break; 62 62} 63 63} 64 64 cout <Endl; 65 65 66 66 cout <"print the first sequence:"; 67 67 for (INT I = 0; I <t_ileftiter; I ++) {68 68 if (t_pleftarr [I]! = 0) {69 69 cout <t_pleftarr [I] <"; 70 70} else {71 71 break; 72 72} 73} 74 74 cout <Endl; 75 75 cout <"the first sequence is:" <t_isumleft <Endl; 76 76 77 77 cout <"print the second sequence :"; 78 78 for (INT I = 0; I <t_irightiter; I ++) {79 if (t_prightarr [I]! = 0) {80 80 cout <t_prightarr [I] <"; 81 81} else {82 82 break; 83 83} 84 84} 85 85 cout <Endl; 86 86 cout <"the second sequence is:" <t_isumright <Endl; 87 87 t_vec.clear (); 88 88 Delete [] t_parr; 89 89 Delete [] t_pleftarr; 90 90 Delete [] t_prightarr; 91 91 system ("pause"); 92 92 return 0; 93 93 94 94 95 95}
But how do you solve the problem if the question is changed to any integer? :)
Programming beauty 2.18 array split original solution O (nlogn) time complexity solution: