Document directory
Timus 1005. Stone Pile requires that several stones be divided into two heaps to minimize the weight difference.
1005. Stone Pile
Time Limit: 2.0 second
Memory limit: 16 MB
You have a number of stones with known Weights
W1 ,...,
Wn. Write a program that will rearrange the stones into two piles such that weight difference between the piles is minimal. inputinput contains the number of stones
N(1 ≤
N≤ 20) and weights of the stones
W1 ,...,
Wn(1 ≤
Wi≤ 100000) delimited by white spaces. outputyour program shocould output a number representing the minimal possible weight difference between stone piles. Sample
Problem Source:USU championship 1997
The answer is as follows:
1 using system;
2 using system. IO;
3 using system. Text. regularexpressions;
4
5 // http://acm.timus.ru/problem.aspx? Space = 1 & num = 1005
6 class acm1005
7 {
8 Static void main ()
9 {
10 new acm1005 (). Run (console. In, console. Out );
11}
12
13 void run (textreader reader, textwriter writer)
14 {
15 writer. writeline (getresult (getweigths (Reader )));
16}
17
18 int [] getweigths (textreader reader)
19 {
20 string [] Ss = RegEx. Split (reader. readtoend (). Trim (), @ "\ s + ");
21 int [] weigths = new int [Int. parse (ss [0])];
22 For (INT I = 0; I <weigths. length; I ++) weigths [I] = int. parse (ss [I + 1]);
23 return weigths;
24}
25
26 int getresult (INT [] weigths)
27 {
28 int n = weigths. Length-1;
29 int result = int. maxvalue;
30 int [] piles = new int [2];
31 For (INT I = (1 <n)-1; I> = 0; I --)
32 {
33 piles [0] = weigths [N];
34 piles [1] = 0;
35 For (Int J = n-1; j> = 0; j --) piles [(I> J) & 1) = 0 )? 1: 0] + = weigths [J];
36 int v = math. Abs (piles [0]-piles [1]);
37 If (result> V) Result = V;
38}
39 return result;
40}
41}
This question is to give you a pile of stones. The total number is between 1 and 20, and the weight of each stone is between 1 and 100,000. You are required to divide the piles of stones into two heaps to minimize the weight difference between them and to output this weight difference.
Because the time limit for this question is 2.0 seconds (this time is very loose), and the input size is not large (the total number of stones cannot exceed 20 ), therefore, I use brute force to search for every possible allocation scheme and find the optimal one.
The input weigths array of the getresult method of rows 26th to 40 in the program records the weight of each stone. The 29th rows result variable is used to record the weight difference between two piles of stones, that is, the output result. The length of the piles array of the int type of row 30th is 2, and its two elements represent the weight of the two piles of stones respectively.
In the program, line N is set to the total number of stones minus one, which has two meanings: first, line N in line 28th (also the last one, because the subscript of the C # array starts from 0) the stone is allocated to the 0th heap (piles [0. Second, brute-force search requires the following 2n possibilities, which is reflected in the loop starting from line 1, variable I decreases from 2n-1 to 0 to traverse these 2n possibilities.
The 35th rows of loops in the program distribute the stones to two heaps Based on the variable I value (which represents every possible allocation scheme of the brute force search, this is determined by whether the J-bit of I is 0. If it is 0, it is allocated to the 1st heap; otherwise, it is allocated to the 0th heap. 36th rows calculate the weight difference between the two piles of stones, and 37th rows are used to update the minimum weight difference.
This algorithm is only capable of accepted (the actual running time of this program is 0.171 seconds, without exceeding the time limit of 2.0 seconds). Her advantage is that it is simple and clear, however, this algorithm is definitely not optimal. If the problem is expanded or the time limit is more stringent, you need to find a better algorithm.