题干:

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

题目大意:给n个人,0表示女生,1表示男生,第i个人的权值是i。女生分两组G1,G2,男生分两组G3,,G4。现在要构造这n个人的分组,使得G1+G3=G2+G4。

解题报告:

换句话说,使其中女生中的一组与男生中的一组中权值的和等于总权值的一半。

不难发现,其实与男女生无关,我们只要将权值分成一半和一半,然后对于前一半这个集合,男生就塞进G3,女生就塞进G1就行了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
char s[MAX];
int bk[MAX];
int main()
{int t;cin>>t;while(t--) {ll n;scanf("%lld",&n);ll ans = (n+1)*n/2;scanf("%s",s+1);if(ans%2 == 1) {printf("-1\n");continue;}ll res = ans/2;//bk=1  代表放入  女1  男3 for(ll i = n; i>=1; i--) {if(res-i >= 0) {bk[i] = 1;res -= i;}else bk[i] = 0;}for(int i = 1; i<=n; i++) {if(s[i] == '1') {//男 if(bk[i] == 1) printf("3");else printf("4");}else {if(bk[i] == 1) printf("1");else printf("2");}}printf("\n"); }return 0 ;
}

【ZOJ - 4033】CONTINUE...? (思维,整体思想,分组思想)相关推荐

  1. CONTINUE...? ZOJ - 4033

    CONTINUE...? ZOJ - 4033 题解:先计算一下总数sum是否是偶数,如果不是则不能分出来,如果是则从后面开始分,先把人分到1.3组,分完sum / 2这些人,如果恰好能够分成零,那么 ...

  2. 冒号课堂:知识之上是思想,思想之上是精神

    http://www.cnblogs.com/xyz98/archive/2009/03/11/1408998.html 1.1开班发言--程序员的四层境界 授人以鱼不如授人以渔            ...

  3. 【转】技术人如何加速成长?提升你的思维和学习方式【思想上勤奋、借事修人、第一性原理、结构化思维/金字塔原理、复盘、解构和重组】

    原创 涵爸 阿里技术 收录于话题  #技术人成长之路 阿里妹导读:每个人都会经历一个新人的阶段,不管是初入职场,还是到一个新的工作环境.作为技术新人,我们常常会面临哪些困惑?普通人和非凡人的差异在哪里 ...

  4. 穿越时空的思想—庄子思想

    庄子思想在每个时代都有它得意义.作用和价值,而不仅仅是指它在我们这个时代的意义.庄子的思想,正如一切伟大先哲的思想一样,具有深刻的当代性,在每个时代都激起思考,引起共鸣,激发出思想的火花.其所以如此, ...

  5. 计算机科学渗透信息论的思想,信息系统思想在高中地理课程教学中的渗透方法分析...

    信息系统思想在高中地理课程教学中的渗透方法分析 系统科学是重要的科学思想,培养学生的系统性思想,对于提高学生的创新能力具有重要实践意义.信息系统是系统科学的重要实践,具有开放性.复杂性.整体性.动态性 ...

  6. ZOJ 3450 Doraemon's Railgun (DP·分组背包)

    题意  多啦A梦有一个超电磁炮  然后要打死n堆敌人  在同一条射线上的敌人只有先打死前面的一堆才能打后面的一堆  给你打死某堆敌人需要的时间和这堆敌人的人数   问你在T0时间内最多打死多少个敌人 ...

  7. HYSBZ - 3309 D - DZY Loves Math(莫比乌斯反演+组合思想+DP思想)*好题。。。

    题目链接:https://cn.vjudge.net/problem/HYSBZ-3309 #include<bits/stdc++.h> using namespace std;#def ...

  8. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  9. while求和java,while语句基本练习(求和思想,统计思想)

    a:循环结构while语句的格式: 初始化语句; while(判断条件语句) { 循环体语句; 控制条件语句; } b:执行流程: a:执行初始化语句 b:执行判断条件语句,看其返回值是true还是f ...

最新文章

  1. Latex中的列表环境[一]
  2. shell批量增删改查百库百表(mysql)
  3. MyBatis 之 动态SQL
  4. CentOS安装Pure-FTPd+MySQL
  5. es6 filter方法_ES5和ES6函数你不知道的区别
  6. python manager详解_python 多进程共享全局变量之Manager()详解
  7. web前端自动化测试工具phantomjs的使用笔记
  8. 使用Secure Store Service连接数据库
  9. 初级程序员考试大纲 (转)
  10. 计算机软件如何永久删除,如何彻底删除电脑软件
  11. 2019-3-5 梦
  12. StudentManagerSystem(学生管理系统)(利用Result类,各种工具类)
  13. 机械键盘轴体(红轴、茶轴、黑轴、青轴)
  14. Linux Windows 的Tensorflow 配置: Nvidia 老显卡运算能力低于3.0
  15. 1.认识华为数据通信
  16. 关于Adams驱动函数单位与符号d的问题
  17. 790-C语言的数组元素下标为何从0开始?
  18. 【Office】Word中的标尺,如何以厘米为单位显示
  19. “同声传译”的难度有多大?
  20. cad2016中选择全图字体怎么操作_CAD2016 软件安装教程

热门文章

  1. 印象笔记mac版 同步问题_印象笔记表示 今年将大幅提升产品体验
  2. PHP用gd库给图片添加水印,php用GD库给图片添加水印
  3. macos php无法访问,Mac上,Apache启动正常,却无法访问localhost和127.0.0.1
  4. 检测同心圆_(二)光线如何被眼睛检测到?
  5. stm32 485和232可以用同一个串口吗_STM32的复用时钟何时开启?
  6. gsoap使用心得! (win32)
  7. A20 网卡驱动分析
  8. 五大算法之三--贪心算法
  9. Linux内核的时钟中断
  10. java se好用吗_利用 Java SE 7 更好地管理资源