zoj4033 思维题

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

Author: LIN, Xi

Source: The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple

题目中的13,24分组纯粹是迷惑,我们只需要把这这些人分成两组用1,0去标记,判断两组人的宝石数是否相等就可以了,如果不相等输出-1,如果相等,那么标记为1的分到1,3组,标记为0的分到2,4组。那么如何分才能相等呢?先判断n是奇是偶,如果是奇数,例如11,那么对于这个公差为1的等差数列来说,1 2 3 ...11  那么被标记的是(1,10)(2,9)(3,8)   另外被标记为0的是(5,6)(4,7)(11)所以这就变成了两个相等的,如果是偶数,例如8,1 2...8 那么被标记1的是(1,8)(2,7),同理为0的是(3,6)(4,5)所以我们可以发现规律,总和要么是n要么是n+1,所以分组的边界应该是(n+1)/4和n/4.至于能否判断相等,那么直接求和公式%2就好了

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;
char c[100009];
int  a[100009];
int main()
{
    int t,i;
     scanf("%d",&t);
    while(t--)
    {
        long long n;
        memset(a,0,sizeof(a));
        scanf("%lld",&n);
         scanf("%s",c+1);
        long long s=(n)*(n+1)/2;
        long long sum=0;
        //printf("s==%lld\n",s);
        if(s%2!=0)
        {
            printf("-1\n");
            continue;
        }
        else
        {
            if(n%2!=0)
            {
                for(i=1; i<=(n+1)/4; i++)
                {
                    a[i]=1;
                    a[n-i]=1;
                }
            }
            else
            {
                for(i=1;i<=n/4;i++)
                {
                    a[i]=1;
                    a[n+1-i]=1;
                }
            }
        }
        for(i=1; i<=n; i++)
        {

            if(a[i]==1)
            {
                if(c[i]=='0') c[i]='1';
                else c[i]='3';
                sum+=i;
            }
            else
            {
                if(c[i]=='0') c[i]='2';
                else c[i]='4';
            }
        }
        if(sum!=s/2) printf("-1\n");
        else printf("%s\n",c+1);
    }

}

猜你喜欢

转载自blog.csdn.net/keepcoral/article/details/80150048