码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 平分宝石(思路题)

时间:2018-05-20 22:46:39      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:eof   multi   span   for   size   specific   tle   not   VID   

Problem H

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 48   Accepted Submission(s) : 16
Special Judge
Problem Description
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

输入一串字符串(01串),si=0表示第i个人是女生,si=1表示第i个人是男生,第i个人获得宝石i个,则总宝石数量是n*(n+1)/2
女生分 1 2 两组 男生分 3 4两组 ,问怎么分配使得sum(1+3) == sum(2+4)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <map>

using namespace std ; 

#define maxn 110000
#define LL long long
int num[maxn] ; 
LL t , n  ; 
char str[maxn] ;
int sex[maxn] ;  
int main(){
    scanf("%lld" , &t) ; 
    
    while(t--){
        scanf("%lld" , &n) ; 
        scanf("%s" , str) ; 
        // 截取性别
        for(int i=1 ; i<=n ; i++){
            sex[i] = str[i-1] - 0 ; 
        }
        // 计算宝石总数
        LL sum = (n+1)*n/2 ; 
        if(sum%2!=0){
            cout<<-1<<endl ; 
            continue ; 
        }

        memset(num , 0 , sizeof(num)) ; 

        LL sum_2 = sum / 2 ; 
        // 完成分组同时平均分开宝石数量
        for(int i=n ; i>0 ; i--){
            if(sum_2 >= i){
                sum_2 -= i ; 
                if(sex[i] == 0 ){
                    num[i] = 1 ; 
                }else{
                    num[i] = 3 ; 
                }
            }else{
                if(sex[i] ==1){
                    num[i] = 4 ; 
                }else{
                    num[i] = 2 ; 
                }
            }
        }

        for(int i=1 ; i<=n ; i++){
            printf("%d" ,num[i] ) ; 
        }
        cout<<endl ; 
    }
    return 0 ; 
}

 

 

HDU 平分宝石(思路题)

标签:eof   multi   span   for   size   specific   tle   not   VID   

原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/9064930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!