ZOJ - 4033 CONTINUE...? (2018浙江省赛 J)

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


题意:给数字分组。如果和为奇数,那么肯定分不了。如果和为偶数,肯定能分,这个时候,贪心的从后往前取数字即可。


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

string str;

int main()
{

    int T;
    scanf("%d",&T);
    ll N;
    while(T--){
        cin>>N;
        cin>>str;
        string ans;
        ans.resize(N,' ');

        if((1+N)*N/2%2==1){
            cout<<-1<<endl;
        }
        else{
            ll sum=(1+N)*N/2/2;


            for(int i=N;i>=1;i--){
                if(sum<=N&&ans[sum-1]==' '){

                    if(str[sum-1]=='1')
                        ans[sum-1]='3';
                    else
                        ans[sum-1]='1';
                    break;
                }

                sum-=i;
                if(str[i-1]=='1')
                    ans[i-1]='3';
                else
                    ans[i-1]='1';
            }

            for(int i=1;i<=N;i++){

                if(ans[i-1]==' '){
                    if(str[i-1]=='1')
                        ans[i-1]='4';
                    else
                        ans[i-1]='2';
                }
            }
            cout<<ans<<endl;

        }


    }

    return 0;
}


//1 2 3 4 5 6 7 8 9 10 11 12 13 14 15




猜你喜欢

转载自blog.csdn.net/lzc504603913/article/details/80145799