Swaps the elements in two sequences A and B to minimize the difference between the sum of sequence a and the sum of sequence B.

Source: Internet
Author: User

Http://blog.csdn.net/v_JULY_v/article/details/6126444 32nd

Problem description:

There are two sequences, A and B, all of which are n. The values of the sequence elements are random integers and unordered;
Requirement: by exchanging elements in A and B, the deviation between [Sum of Element A] and [Sum of Element B] is minimized.

CodeImplementation:

# Include <cstdio> # include <cstdlib> # include <cmath> void swapi (int * X, int * Y); int sum (int A [], int N ); int isxina (int x, int A); void process (int A [], int B [], int N); int main () {int A [] = {1, 2, 3, 29, 30}; int B [] = {1,210,232,123 11, 12312}; int n = 5; Process (A, B, n ); for (INT I = 0; I <n; I ++) printf ("% d", a [I]); printf ("\ n "); for (INT I = 0; I <n; I ++) printf ("% d", B [I]); printf ("\ n % d \ n ", ABS (sum (A, n)-sum (B, n); System ("pause"); Return 0;} void swapi (int * X, int * Y) {int temp = * X; * x = * Y; * Y = temp;} int sum (int A [], int N) {int ret = 0; for (INT I = 0; I <n; I ++) RET + = A [I]; return ret;} int isxina (INT X, int) {If (x> 0 & x <A) | (x> A & x <0) return 0; return 1 ;} void process (int A [], int B [], int N) {int sum_a = sum (A, n); int sum_ B = sum (B, n ); int A = sum_a-sum_ B; float minvalue = ABS (A [0]-B [0]-A/2.0); int II = 0, JJ = 0; int flag = 0; if (a = 0) return; For (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {int x = A [I]-B [J]; if (x = 0) continue; If (isxina (X, A) = 0) {float temp = (float) ABS (A [I]-B [J]-A/2.0); If (temp <minvalue) {minvalue = temp; II = I; JJ = J; flag = 1 ;}}}if (flag = 1) {swapi (& A [II], & B [JJ]); Process (A, B, n ); // continue solving} // solution thought: // The sum of the current array A and array B is // A = sum (a)-sum (B) /// After the I-th element and the J-th element of B are exchanged, the sum of A and B is // a' = sum () -A [I] + B [J]-(sum (B)-B [J] + A [I]) // = sum (a)-sum (B) -2 (A [I]-B [J]) // = A-2 (A [I]-B [J]) /// set X = A [I]-B [J] // | A |-| a' | = | A |-| A-2x | // assume> 0, // when X is between (0, A), such an exchange can reduce the sum of A and B after the exchange, // The Closer X is to A/2, the better the effect. // If X cannot be found between (0, A), the current A and B are the answer. /// SoAlgorithm Probably as follows: // in A and B, find the I and j that make X Between (0, A) and closest to a/2, and exchange the corresponding I and j elements, // After recalculating A, repeat the previous step until the X Between (0, a) is not found.

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.