CONTINUE...?

Topic link: Portal

Title:

Four groups, G1, G2, G3, G4, G1 and G2 can only be divided into girls, G3 and G4 can only be divided into boys.

Asked whether the distribution is reasonable, the number of gems in the hands of the people in the G1 and G3 groups is the same as the number of gems in the hands of the people in the G2 and G4 groups.

Input data analysis:

n = 7, this set of data indicates a total of 7 individuals.

1101001. The subscript of this string starts from 1, such as a[2] = 1, 1 indicates that this person is a boy, if a[2] = 0 indicates that this person is a girl, he is the second person, and he has two pieces in his hand Gems, there are several gems for the first person, and the subscript indicates which person he or she is.

If there are multiple grouping methods, just output any one .

Ideas:

Because G1 and G3 can be divided into males and females, so can G2 and G4.

Therefore, there is no need to distinguish between men and women, and this problem can be simplified into gems.

The simplified problem is as follows: There are 1, 2, 3, .... n. There are such n piles of gems, and they are equally divided into two pieces. Each pile of gems cannot be split.

The total number of gems sum = n*(n+1)/2 , the sum must be an even number, if it is an odd number, it cannot be divided equally.

From sum must be even, we can get n%4 == 0 || (n+1)%4 == 0 (please think for yourself)

Two cases are analyzed separately:

(1) n%4 == 0 , the method of dividing gems can be fixed, 

Take an example 1, 2, 3, 4, 5, 6, 7, 8. One pile (G1, G3): 1, 8, 3, 6 another pile (G2, G4) 2, 7, 4, 5

(2) (n+1) % 4 == 0   <==>   (n-3) % 4 == 0

Divide the first three piles of gems, 1, 2 to G1, G2, 3 to 3. The rest is divided according to the previous method.

The grouping has been completed, and the method output only needs to consider whether he is a male or a female.


Reference code: The code was written during the practice game, which is a bit messy, but I feel that the idea of ​​this question is the most important, so I won't rewrite the code.

#include <iostream>
#include <algorithm>

using namespace std;
const int N = 100010;

char a[N];
char b[N];

intmain()
{
    int T, n;
    cin >> T;
    while(T--) {
        cin >> n;
        cin >> a;
        long long ans = n*(n+1)/2;
        if(n <= 2 || ans%2 == 1) {
            cout << "-1" << endl;
        }
        else {
            if(n % 2 == 0) {
                for(int i=0; i<=n/2-1; i++) {
                    int j = n-i-1;
                    if((i+1)%2 == 1) {
                        if(a[i] == '1')
                            b[i] = '3';
                        else
                            b[i] = '1';

                        if(a[j] == '1')
                            b[j] = '3';
                        else
                            b[j] = '1';
                    }
                    else {
                         if(a[i] == '1')
                            b[i] = '4';
                        else
                            b[i] = '2';

                        if(a[j] == '1')
                            b[j] = '4';
                        else
                            b[j] = '2';
                    }
                }
            }
            else {
                if(a[0] == '1')
                    b[0] = '3';
                else
                    b[0] = '1';

                if(a[1] == '1')
                    b[1] = '3';
                else
                    b[1] = '1';

                if(a[2] == '1')
                    b[2] = '4';
                else
                    b[2] = '2';
             for(int i=3; i<3+(n-3)/2; i++) {
                    int j = n-(i-2);
                    if((i+1)%2 == 1) {
                        if(a[i] == '1')
                            b[i] = '3';
                        else
                            b[i] = '1';

                        if(a[j] == '1')
                            b[j] = '3';
                        else
                            b[j] = '1';
                    }
                    else {
                         if(a[i] == '1')
                            b[i] = '4';
                        else
                            b[i] = '2';

                        if(a[j] == '1')
                            b[j] = '4';
                        else
                            b[j] = '2';
                    }
                }
            }
            b[n] = '\0';
            cout << b << endl;
        }
    }
    return 0;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326074247&siteId=291194637