HDU 平分宝石(思路题)

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 ;
}

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

时间: 2024-10-11 04:11:51

HDU 平分宝石(思路题)的相关文章

hdu 5701(区间查询思路题)

中位数计数 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 999    Accepted Submission(s): 383 Problem Description 中位数定义为所有值从小到大排序后排在正中间的那个数,如果值有偶数个,通常取最中间的两个数值的平均数作为中位数. 现在有n个数,每个数都是独一无二的,求出每个数在多少个包

hdu 4961 数学杂题

http://acm.hdu.edu.cn/showproblem.php?pid=4961 先贴个O(nsqrtn)求1-n所有数的所有约数的代码: vector<int>divs[MAXN]; void caldivs() { for(int i=1;i<MAXN;i++) for(int j=i;j<MAXN;j+=i) divs[j].push_back(i); } 有了这个当时理下思路就可写了,但是重复数处理注意: 1.用一个数组vis[]  vis[i]=1表示i存在

HDU Multiplication table 阅读题

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4951 题意:给一个P进制的乘法表,行和列分别代表0~p-1,第i行第j*2+1和第j*2+2列代表的是第i行的数x和第j列的数的乘积,不过这个乘法表被人弄乱了,原来的0~p-1被映射到了一个新的0~p-1的permutation(以前在CF上看见的permutation表示1~P,所以稍有迷茫),分别代表了新的不同的数,乘法表上的数也都被映射了.现在要找出映射到了什么新的数. 思路:毫无疑问,出现最多的

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B&quo

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

HDU 2092 整数解 --- 水题

x+y = n, x*y = m; y = n - x; x * ( n - x) = m nx - x^2 = m; x^2 - nx + m = 0; △ = sqrt(n^2 - 4m) 要有整数解即△需要为可开方数即可. /* HDU 2092 整数解 --- 水题 */ #include <cstdio> #include <cmath> int main() { double n, m; while (scanf("%lf%lf", &n,

HDU 4279 Number 规律题

题意: 定义函数F(x) : 区间[1,x]上的y是满足:GCD(x,y)>1 && x%y>0的 y的个数. 问:对于任意区间[l,r] 上的F(l···r) 有几个函数值是奇数的. 打表找规律. 打的是[1,x]区间的结果 把所有结果不相同的值打出来(因为结果是递增的,所以只观察不相同的结果) 发现:ans = x/2-2 || x/2-1 再把所有结果不同 && x/2-1的值打出来 发现 sqrt(x) &1 == 1 得到:ans = x/2-

hdu 1711 KMP模板题

// hdu 1711 KMP模板题 // 贴个KMP模板吧~~~ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int MAX_N = 1000008; const int MAX_M = 10008; int T[MAX_N]; int p[MAX_M]; int f[MAX_M]; int

51nod P1305 Pairwise Sum and Divide ——思路题

久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1305<< 有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整: fun(A) sum = 0 for i = 1 to A.length for j = i+1 to A.length sum = sum + Floor(