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

Author: LIN, Xi

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

题意:给n个人,0表示女生,1表示男生。女生分两组,男生分两组。使其中女生中的一组与男生中的一组中权值的和等于总权值的一半。

思路:易发现第i个权值与第n-i个权值的和为n,所以依次找出规律:

把选中的男生和女生 分组,选中的标记为0,没选的标记为1;

以样例四为例:

权值: 1 2 3 4 5 6 7

原始: 1 1 0 1 0 0 1

分组 :  3 4 1 3 2 1 4

标记: 0 1 0 1 0 1 0

 

当 0<=i<n/2 时,将标记为0的男生分到3组,女生分到1组;

当  i >= n/2  时,将标记为1的男生分到3组,女生分到一组;

分别对1、3组和2、4组的权值求和,若不相等输出-1,否则输出位置。

 

#include <cstdio>
#include <algorithm>
#include <functional>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
const int maxn=1e6+10,inf=(1<<30),mod=1e9+7;
char que[maxn];
int place[maxn];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        scanf("%s",que);
        int sum1=0,sum2=0;
        for (int i=0;i<n/2;i++){
            if(i%2==0) place[i]=0,sum1+=i+1;
            else place[i]=1,sum2+=i+1;
        }
        for (int i=n/2;i<n;i++){
            if(i%2==0) place[i]=1,sum2+=i+1;
            else place[i]=0,sum1+=i+1;
        }
        if(sum1!=sum2){
            printf("-1\n");
            continue;
        }
        for (int i=0;i<n;i++){
            if(que[i]=='1'){
                if(place[i]) printf("4");
                else printf("3");
            }
            else {
                if(place[i]) printf("2");
                else printf("1");
            }
        }
        printf("\n");
    }
    return 0;
}
View Code

 

posted @ 2018-07-25 16:20  Alwayskoo  阅读(174)  评论(0编辑  收藏  举报