A number set division problem that has plagued me for a long time

Source: Internet
Author: User

I remember that I had a question on toJ, which roughly meant the following:

 

Divide the 2N integers into two heaps equally, N for each heap, so that the difference between the two heap and is minimized.

 

At that time, I was proud to use the "random greedy" idea to write the program top1 on OJ (44 K 0 ms)

Looking at the memory usage of several hundred kb + tens of milliseconds of computing speed of a large number of contestants using dp ac, I'm proud of it.

 

The random greedy idea is as follows:

 

1 divide the 2N number into two heaps at will, called a and B.

2 If a in a, B in B exists, exchange A and B to make the difference between sum (A) and sum (B) Smaller, then exchange.

3. If 2 does not exist, jump out; otherwise, Repeat 2.

4. Obtain the minimum sum difference.

 

Then I thought about it myself. What if the question is changed to this? --

 

Divide n integers into two heaps. The number of each heap is not fixed, so that the two heaps and the difference value are minimized.

 

Then I discussed the problem with N students .. Fail. Finally, we come to the conclusion that this is an NP problem ......

I can still use random greed to obtain an approximate solution --

 

0 r = positive infinity

1. Randomly divide n numbers into two heaps: A and B.

2 if there is a in a or B in B, Put A and B in another set, so that the difference between sum (A) and sum (B) is smaller, then change.

3. If 2 does not exist, jump out; otherwise, Repeat 2.

4. Obtain the minimum sum difference K in a single time. If the sum difference is smaller, r = K

5 random times + +

6 if the number of random times> N (the higher the value, the closer it is to the correct solution), the output R

7 to 1

 

It looks good. In theory, the greater N, the more accurate the solution.

 

I once thought this was a good solution .. Until WL proposed a better solution ......

 

1 sorts n numbers, called set

2. Take out the two largest numbers A, B, and subtract C from each other.

3 A, B, and C

4. If a only jumps out of one element, otherwise it jumps to 2.

5. The last element in a is correct.

 

Depend on me ~ After thinking about it, we can solve this problem with a common greedy algorithm ......

In the final analysis, it is still a mathematical problem. If the Division is not evenly divided, the numbers can be "equivalent. (The proof here is omitted, obviously ......)

 

Therefore, it may not seem that the "niu X" algorithm is Niu X. All algorithm problems should be rooted in its mathematical nature.

-- Always alert yourself with this sentence! I have already made numerous mistakes on this issue.

 

PS: I asked this question on the recruitment pen questions of the company's Tianjin University/Nankai University campus. On the spot, I gave an approximate solution of the first question to only one master student .. No one answered the second question .. (Time may not be enough)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.