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

题干:

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

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440402.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android 仿真翻页动画,Android 两Activity之间动画效果(1)---------翻页效果

用Android rotate动画实现翻页效果&#xff0c;效果如图&#xff1a;要实现上面动画&#xff0c;首先搞明白rotate动画原理&#xff1b;(1)Degrees坐标&#xff1a;0度(360度)270度90度 顺时针旋转 180(2)rotate 关键属性fromDegrees 开始旋转时角度 toDegrees 结束时的角…

android 存储不被垃圾清理,手机内存足够大,就不需要清理垃圾了?你错了!

原标题&#xff1a;手机内存足够大,就不需要清理垃圾了?你错了!中新网4月20日电今天,人们使用智能手机的时间已超过电脑,希望在任何时候、任何地方,一部手机搞定所有。对手机的流畅度、性能和安全的要求越来越高。新手机刚到手时非常流畅,用一段时间就出现各种卡顿,网民对猎豹…

【蓝桥杯 - 真题】六角幻方(dfs+剪枝)

标题&#xff1a;六角幻方 把 1 2 3 ... 19 共19个整数排列成六角形状&#xff0c;如下&#xff1a; * * * * * * * * * * * * * * * * * * * 要求每个直线上的数字之和必须相等。共有15条直线哦&#xff01; 再给点线索吧&#xff01;我们预先填好了2个数字&…

【2019浙江省赛 - J】Welcome Party(并查集,bfs,优先队列,建图)

题干&#xff1a; The 44th World Finals of the International Collegiate Programming Contest (ICPC 2020) will be held in Moscow, Russia. To celebrate this annual event for the best competitive programmers around the world, it is decided to host a welcome pa…

app android de,Android Deobfuscation

Android Deobfuscation11/11/2019本文内容ProGuard、DexGuard 和 R8 是用于对 Android 应用程序的代码进行优化和模糊处理的工具。 它会删除未使用的代码、重命名具有语义模糊名称的类、字段和方法&#xff0c;使基本代码更小&#xff0c;更难反向工程。 若要在 Android 应用中…

【2019浙江省赛 - B】Element Swapping(思维,数学)

题干&#xff1a; DreamGrid has an integer sequence and he likes it very much. Unfortunately, his naughty roommate BaoBao swapped two elements and () in the sequence when DreamGrid wasnt at home. When DreamGrid comes back, he finds with dismay that his …

android 没有指令,android – 运行时没有命令输出:’am start -n

最近,当我试图在我的设备上运行我的Android应用程序时,我必须在实际启动之前从Eclipse运行它几次.我试图重新安装JRE,JDK和IDE,我试图切换工作区.我还让Eclipse为Java VM使用更多的RAM.我的IDE,JRE,JDK,ADT和ADT插件都是最新的.对此有任何建议非常感谢.No command output when …

【2019浙江省赛 - E】Sequence in the Pocket(思维)

题干&#xff1a; DreamGrid has just found an integer sequence in his right pocket. As DreamGrid is bored, he decides to play with the sequence. He can perform the following operation any number of times (including zero time): select an element and move i…

android和ios系统的内存,WP和Saipan系统的流畅程度相当于ios,占用的内存很少,但是为什么要用Android取代它...

当涉及到WP和Symbian系统时&#xff0c;许可能没有听说过它&#xff0c;但是对于大多数关注智能手机市场增长的消费者来说&#xff0c;它已经为人们所熟悉&#xff0c;并且许已经使用了它. 当时在功能性机器上使用了Saipan系统&#xff0c;但是您会发现该系统的流畅性与当时的i…

【CodeForces - 1042C】Array Product(思维,有坑细节)

题干&#xff1a; You are given an array aa consisting of nn integers. You can perform the following operations with it: Choose some positions ii and jj (1≤i,j≤n,i≠j1≤i,j≤n,i≠j), write the value of ai⋅ajai⋅aj into the jj-th cell and remove the num…

红米pro android o刷机,红米Pro如何刷机?你可以通过这两种方法获取root权限!

小米官网最近发布了关于红米pro的消息&#xff0c;相信很多米粉们已经上手了&#xff0c;那么新到手的机子怎么刷机呢&#xff1f;下面小编为大家带来一个完整的红米Pro官方卡刷机教程&#xff0c;希望可以帮助到大家。红米Pro卡刷升级教程&#xff1a;准备工作1.进入红米Pro刷…

【2019浙江省赛 - K 】Strings in the Pocket(马拉车,思维)

题干&#xff1a; BaoBao has just found two strings and in his left pocket, where indicates the -th character in string , and indicates the -th character in string . As BaoBao is bored, he decides to select a substring of and reverse it. Formally spe…

android+微信一键关注,一键关注微信公众平台JS代码有哪些?

一键关注微信公众平台JS代码有哪些&#xff1f;在网页设置一个按钮或者链接可以让用户一键关注微信公众平台&#xff0c;那么这种一键关注微信公众平台的功能如何实现呢&#xff1f;下面小编分享给大家一键关注微信公众平台的JS代码。在微信上&#xff0c;通过微信公众平台推送…

【2019浙江省赛 - A】Vertices in the Pocket(权值线段树下二分,图,思维)

题干&#xff1a; DreamGrid has just found an undirected simple graph with vertices and no edges (thats to say, its a graph with isolated vertices) in his right pocket, where the vertices are numbered from 1 to . Now he would like to perform operations …

html写原生曲线图,HTML5 平滑的正弦波曲线图

JavaScript语言&#xff1a;JaveScriptBabelCoffeeScript确定var waves new SineWaves({el: document.getElementById(waves),speed: 4,width: function() {return $(window).width();},height: function() {return $(window).height();},ease: SineInOut,wavesWidth: 80%,wav…

【CodeForces - 1150C】Prefix Sum Primes(思维)

题干&#xff1a; Were giving away nice huge bags containing number tiles! A bag we want to present to you contains nn tiles. Each of them has a single number written on it — either 11or 22. However, there is one condition you must fulfill in order to re…

asp.net 写入html代码,asp.net读取模版并写入文本文件

本文要介绍的是ASP.NET怎样读写文本文件&#xff0c;但更重要的是实现的过程。使用的工具是Visual Studio 2015 &#xff0c;.NET版本是4.6.1 。一共建立的2个项目&#xff0c;HoverTreePanel和HoverTreeWeb&#xff0c;都是ASP.NET项目。文章末尾附源码下载。项目结果如下图&a…

【CodeForces - 1150A】Stock Arbitraging (贪心,水题)

题干&#xff1a; Welcome to Codeforces Stock Exchange! Were pretty limited now as we currently allow trading on one stock, Codeforces Ltd. We hope youll still be able to make profit from the market! In the morning, there are nn opportunities to buy share…

html以图像中心定位,在HTML图像上水平和垂直居中文本(绝对定位)

Michael Roach0htmlcssflexbox鉴于以下设计元素,我试图在html中包含图像,以便可以使用css过渡(悬停效果)操纵不透明度.这里的主要缺点是我使用手动垂直居中(绝对/顶部:4​​0%),这在缩小浏览器时变得明显.在使用绝对定位时,是否可以使用flexbox或table进行垂直居中&#xff1f;…

*【CodeForces - 1150D】Three Religions(dp,预处理,思维)

题干&#xff1a; During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information on the evolution of each of these beliefs, and you now…