The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - J 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

原题地址:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5759
题意:
老师分宝石,一个由0和1组成的字符串数组,每个字符都是一个学生每个学生s[i]都有i个宝石,其中1是男生,0是女生
有G1G2G3G4四个分组其中G1G2只能女生,G2G3只能男生。要求G1G3一组,G2G4一组,让他们两组的宝石个数相同

思路:
一个数组能否被分成两堆第一步是取决于他们的和是否为偶数,如果是奇数是不可能分成两堆的。
题目有说可以让有的组数为0所以男生女生这个要求就只是一个干扰项;
具体:首先我们判断长度为偶数的情况
例如:
8
11111111
这个数据
首先他们的值是
1 2 3 4 5 6 7 8
我们让第一个”1“为第A组,第二个”2“为第B组 这样第A组的值减去第B组的值为(1-2)=-1;
然后我们让第三个”3“为第B组,第四个”4“为第A组,这样第A组减去第B组的值为(4-3)=1;
正好抵消为0就这样依次 A B B A A B B A就能保证他们的合为0;

然后我们看奇数长度的情况
例如:题目的最后一组数据
7
1101001
他们的值是
1 2 3 4 5 6 7
首先我们从第二个开始
第二个在A组,第三个在B组 这时候A-B=(2-3)=-1;
第四个在B组,第五个在A组 这时候A-B=(5-4)=1; 这时前面四个正好抵消
然后第六个在A组,第四个在B组,这时候A-B=(6-7)=-1;
这时候正好可以把前面被忽略的1加进来放在不够的A组上这时候正好抵消;

代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int one;
int two;
int three;
int four;
int mark[200005];
bool yes(ll n){
        if(((n*(n+1))/2)%2==1)return false;
        return true;
}
int main()
{
        //std::ios::sync_with_stdio(false);
        ll n;
        int t;
        cin>>t;
        string s;
        while(t--){
                cin>>n;
                cin>>s;
                if(!yes(n)){//判断是否合为偶数
                        cout<<-1<<endl;
                        continue;
                }
                if(n%2==0){///长度为偶数的情况
                        for(int i=0;i<n;i++){
                                if(i%4==0||i%4==3){
                                        if(s[i]=='1')cout<<"3";
                                        else cout<<"1";
                                }
                                else if(i%4==1||i%4==2){
                                        if(s[i]=='0')cout<<"2";
                                        else cout<<"4";
                                }
                        }
                }
                else{///长度为奇数的情况
                        if(s[0]=='1')cout<<"3";
                        else cout<<"1";
                        for(int i=1;i<n;i++){
                                if((i-1)%4==0||(i-1)%4==3){
                                        if(s[i]=='1')cout<<"3";
                                        else cout<<"1";
                                }
                                else if((i-1)%4==1||(i-1)%4==2){
                                        if(s[i]=='0')cout<<"2";
                                        else cout<<"4";
                                }
                        }
                }
                cout<<endl;
        }
    return 0;
}

 




posted @ 2018-04-30 01:54  luowentao  阅读(525)  评论(0编辑  收藏  举报