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

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划分成两部分(男女分组是干扰的)

1 2 3 4 5 6 7 8
偶数-->1+8+2+7 = 3+6+4+5

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

偶数: n只需要前后匹配即可

奇数: n/2之前的奇数位和n/2之后的偶数位相加,等于n/2之前的偶数位加n/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;
}
posted @ 2018-04-29 21:20  Roni_i  阅读(262)  评论(0编辑  收藏  举报