1. 程式人生 > >CONTINUE...?【構造/分析】

CONTINUE...?【構造/分析】

def tails pro elong %s specific ber exce ace

CONTINUE...?
Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge
DreamGrid has  classmates numbered from  to . Some of them are boys and the others are girls. Each classmate has some gems, and more specifically, the -th classmate has  gems.

DreamGrid would like to divide the classmates into four groups , ,  and  such that:

Each classmate belongs to exactly one group.

Both  and  consist only of girls. Both  and  consist only of boys.

The total number of gems in  and  is equal to the total number of gems in  and .

Your task is to help DreamGrid group his classmates so that the above conditions are satisfied. Note that you are allowed to leave some groups empty.

Input
There are multiple test cases. The first line of input is an integer  indicating the number of test cases. For each test case:

The first line contains an integer  () -- the number of classmates.

The second line contains a string  () consisting of 0 and 1. Let  be the -th character in the string . If , the -th classmate is a boy; If , the -th classmate is a girl.

It is guaranteed that the sum of all  does not exceed .

Output
For each test case, output a string consists only of {1, 2, 3, 4}. The -th character in the string denotes the group which the -th classmate belongs to. If there are multiple valid answers, you can print any of them; If there is no valid answer, output "-1" (without quotes) instead.

Sample Input
5
1
1
2
10
3
101
4
0000
7
1101001
Sample Output
-1
-1
314
1221
3413214

【題意】:https://www.cnblogs.com/bluefly-hrbust/p/8971769.html

本題題意就是把1到n數表示為,兩組數之和相等,即能不能1->n劃分成兩部分(男女分組是幹擾的)

graph LR
偶數-->1+8+2+7=3+6+4+5

奇數-->1+3+4+6=2+5+7

偶數: N只需要前後匹配即可

奇數: len/2之前的奇數位和len/2之後的偶數位相加,等於len/2之前的偶數位加len/2的奇數位

【分析】:ACZone+

【出處】:CodeForces - 899C Dividing the numbers

【代碼】:

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
    int t,n,len;
    char a[100050];
    int  b[100050];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        getchar();
        scanf("%s",&a);
        len=strlen(a);
        if ((n%4)<=2 && n%4!=0)
        {
            printf("-1\n");
        }
        else
        {
            if(n%2==0) //偶數
            {
                for(int i=0; i<n/4; i++)
                {
                    if (a[i]=='1')printf("3");
                    else printf("1");
                }
                for (int i=n/4; i<n-n/4; i++)
                {
                    if (a[i]=='1')printf("4");
                    else printf("2");
                }
                for (int i=n-n/4; i<n; i++)
                {
                    if (a[i]=='1')printf("3");
                    else printf("1");
                }
                printf("\n");
            }
            else //奇數
            {
                for(int i=0; i<n/2; i++)
                {
                    if (a[i]=='1')
                    {
                        if ((i+1)%2==1)printf("3");
                        else printf("4");
                    }
                    else
                    {
                        if ((i+1)%2==1)printf("1");
                        else printf("2");
                    }
                }
                for(int i=n/2; i<len; i++)
                {
                    if (a[i]=='1')
                    {
                        if ((i+1)%2==1)printf("4");
                        else printf("3");
                    }
                    else
                    {
                        if ((i+1)%2==1)printf("2");
                        else printf("1");
                    }
                }
                printf("\n");
            }
        }
    }
    return 0;
}

CONTINUE...?【構造/分析】