CONTINUE...? ZOJ - 4033

https://vjudge.net/problem/ZOJ-4033/origin

https://vjudge.net/contest/399385#problem/J

DreamGrid has $n$ classmates numbered from $1$ to $n$. Some of them are boys and the others are girls. Each classmate has some gems, and more specifically, the $i$-th classmate has $i$ gems.

DreamGrid would like to divide the classmates into four groups $G_1$, $G_2$, $G_3$ and $G_4$ such that:

  • Each classmate belongs to exactly one group.

  • Both $G_1$ and $G_2$ consist only of girls. Both $G_3$ and $G_4$ consist only of boys.

  • The total number of gems in $G_1$ and $G_3$ is equal to the total number of gems in $G_2$ and $G_4$.

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 $T$ indicating the number of test cases. For each test case:

The first line contains an integer $n$ ($1 \le n \le 10^5$) -- the number of classmates.

The second line contains a string $s$ ($|s| = n$) consisting of 0 and 1. Let $s_i$ be the $i$-th character in the string $s$. If $s_i = 1$, the $i$-th classmate is a boy; If $s_i = 0$, the $i$-th classmate is a girl.

It is guaranteed that the sum of all $n$ does not exceed $10^6$.

Output

For each test case, output a string consists only of {1, 2, 3, 4}. The $i$-th character in the string denotes the group which the $i$-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


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
#define debug(x) cout<<"debug:"<<#x<<" = "<<x<<endl;

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=1e6+10;
const int mod=1e9+7;

char a[100005];


int main()
{
    int T = 0;
    cin >> T;
    while(T--)
    {
        bool sex[100005] = {0};
        bool mark[100005] = {0};
        ll n = 0;
        cin >> n;
        cin >> a;
        if(n == 1 || n == 2)
        {
            cout << -1 << endl;
            continue;
        }
        ll M = 0 , F = 0;
        ll sum = (1+n)*n/2;
        if(sum % 2)
        {
            cout << -1 << endl;
            continue;
        }
        sum /= 2;
        for(ll i = 0;i<n;i++)
        {
            sex[i] = a[i]-'0';
        }
        
        ll s = 0;
        ll p = 0;
        ll sub = 0;
        for(ll i = 0;i<n;i++)
        {
            s += i+1;
            mark[i] = 1;
            if(s > sum)
            {
                s -= i+1;
                p = i;
                sub = sum - s;
                break;
            }
        }
        //cout << p << " " << sub <<endl;
        mark[p-sub] = 0;
        
        //for(int i = 0;i<n;i++)
        //{
        //    cout << mark[i];
        //}cout << endl;
        
        for(ll i = 0;i<n;i++)
        {
            if(sex[i] == 1 && mark[i] == 1)cout << 3;
            if(sex[i] == 1 && mark[i] == 0)cout << 4;
            if(sex[i] == 0 && mark[i] == 1)cout << 1;
            if(sex[i] == 0 && mark[i] == 0)cout << 2;
        }
        cout << endl;
    }
    return 0;
}

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <bitset>#include <cassert>#include <cctype>#include <cmath>#include <cstdlib>#include <ctime>#include <deque>#include <iomanip>#include <list>#include <map>#include <queue>#include <set>#include <stack>#include <vector>#include <iterator>#include <utility>#include <sstream>#include <limits>#include <numeric>#include <functional>usingnamespacestd; #define gc getchar()#define mem(a) memset(a,0,sizeof(a))#define debug(x) cout<<"debug:"<<#x<<" = "<<x<<endl;#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);typedeflonglong ll; typedefunsignedlonglong ull; typedeflongdouble ld; typedef pair<int,int> pii; typedefchar ch; typedefdouble db; constdouble PI=acos(-1.0); constdouble eps=1e-6; constint inf=0x3f3f3f3f; constint maxn=1e5+10; constint maxm=100+10; constint N=1e6+10; constint mod=1e9+7; char a[100005]; int main() { int T = 0; cin >> T; while(T--) { bool sex[100005] = {0}; bool mark[100005] = {0}; ll n = 0; cin >> n; cin >> a; if(n == 1 || n == 2) { cout << -1 << endl; continue; } ll M = 0 , F = 0; ll sum = (1+n)*n/2; if(sum % 2) { cout << -1 << endl; continue; } sum /= 2; for(ll i = 0;i<n;i++) { sex[i] = a[i]-'0'; } ll s = 0; ll p = 0; ll sub = 0; for(ll i = 0;i<n;i++) { s += i+1; mark[i] = 1; if(s > sum) { s -= i+1; p = i; sub = sum - s; break; } } //cout << p << " " << sub <<endl; mark[p-sub] = 0; //for(int i = 0;i<n;i++)//{// cout << mark[i];//}cout << endl;for(ll i = 0;i<n;i++) { if(sex[i] == 1 && mark[i] == 1)cout << 3; if(sex[i] == 1 && mark[i] == 0)cout << 4; if(sex[i] == 0 && mark[i] == 1)cout << 1; if(sex[i] == 0 && mark[i] == 0)cout << 2; } cout << endl; } return0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YukiRinLL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值