CONTINUE...?模拟分情况
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
有些关键信息无非复制。。。没办法大家理解一下
本题题意即为把[1,n]的数放入4个集合中,满足G1+G3=G2+G4,并且有一个01序列与序列1 2 3 4 ....n相匹配,其中0表示此位所代表的值只能放入G1或G2而1代表值放入G3或者G4中。如果存在这样的序列输出
1到N序列中每个元素对应装入G的下标。
其实简化一下,本题题意就是把1到N数表示为 某些数相加等于另外某些数
我们可以写一下
3:1+2=3;
4:1+4=2+3;
5:无法写出
6:无法写出
7:1+3+4+6=2+5+7
8:1+8+2+7=3+6+4+5
为什么5 6无非写出呢?我们发现里面有奇数个奇数,而这样必然会让一边为奇数一边为偶数肯定不行,因此(N%4<=2 && N%4!=0)输出-1;
我们再分N%=2和N%2=1
当N%2==0的时候只可能是N%4==0的情况。。。因为我们淘汰了N%4==2的情况,这样N只需要前后匹配即可
当N%2==1的时候,我又找了一波规律如何奇数个组合
我发现
7:1+3+4+6=2+5+7
11:1+3+5+6+8+10=2+4+7+9+11==33(不信可以算算)
于是我发现。。。貌似len/2之前的奇数位和len/2之后的偶数位相加,等于len/2之前的偶数位加len/2的奇数位
于是我们就能解决把1到N的数组合并且相加成等式的问题了,最后只需要把01分组即可
AC代码
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int t,n,len;
char a[];
int b[];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
getchar();
scanf("%s",&a);
len=strlen(a);
if ((n%)<= && n%!=)
{
printf("-1\n");
}
else
{
if(n%==)
{
for(int i=; i<len/; i++)
{
if (a[i]=='')printf("");
else printf("");
}
for (int i=len/; i<len-len/; i++)
{
if (a[i]=='')printf("");
else printf("");
}
for (int i=len-len/; i<len; i++)
{
if (a[i]=='')printf("");
else printf("");
}
printf("\n");
}
else
{
for(int i=; i<len/; i++)
{
if (a[i]=='')
{
if ((i+)%==)printf("");
else printf("");
}
if (a[i]=='')
{
if ((i+)%==)printf("");
else printf("");
}
}
for(int i=len/; i<len; i++)
{
if (a[i]=='')
{
if ((i+)%==)printf("");
else printf("");
}
if (a[i]=='')
{
if ((i+)%==)printf("");
else printf("");
}
}
printf("\n");
}
}
}
return ;
}
CONTINUE...?模拟分情况的更多相关文章
- HNU 12833 Omar’s Bug(分情况讨论)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12833&courseid=268 解题报告:有个11个 ...
- UESTC 1034 AC Milan VS Juventus 分情况讨论
AC Milan VS Juventus Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Oth ...
- HDU6444(子段和、分情况比较)
要点 不难想到gcd一下然后枚举每个开头走一圈,并记录一下数值. 最终答案是分情况的:1.能走几圈走几圈然后加上最后剩余的最大子段和:2.也可能是最后一圈后面的拖后腿了,所以最后一圈没走完就停,即长度 ...
- bzoj2756: [SCOI2012]奇怪的游戏(网络流+分情况)
2756: [SCOI2012]奇怪的游戏 题目:传送门 题解: 发现做不出来的大难题一点一个网络流 %大佬 首先黑白染色(原来是套路...)染色之后就可以保证每次操作都一定会使黑白各一个各自的值加1 ...
- bzoj 1067 分情况讨论
这道题考察人的严谨,各种情况分类讨论. #include <cstdio> #include <algorithm> #include <map> #define ...
- CODE FESTIVAL 2017 qual A--C - Palindromic Matrix(模拟所有情况,注意细节)
个人心得:其实本来这题是有规律的不过当时已经将整个模拟过程都构思出来了,就打算试试,将每个字符和总和用优先队列 装起来,然后枚举每个点,同时进行位置标志,此时需要多少个点的时候拿出最大的和出来,若不满 ...
- chales抓包,模拟异常情况
抓包能做什么? 1 .可以抓取客户端和server的请求和返回,可以借助判断是客户端问题是server问题 2.可以模拟各种异常情况用来测试异常情况 3.无接口文档情况下测试接口 怎么修改你抓到的请求 ...
- 2018.11.07 NOIP模拟 分糖果(贪心)
传送门 考虑 n = 2 时的情况:假定两个人分别为(a, b),(c, d),则当且仅当min(a,d) ≤ min(b,c)时,把(a, b)放在前面更优,否则把(c, d)放在前面更优 然后把n ...
- Valid Number——分情况讨论最经典的题(没细看)——这题必须静下心来好好看看
Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...
随机推荐
- 报错org.apache.hadoop.mapreduce.lib.input.FileSplit cannot be cast to org.apache.hadoop.mapred.FileSplit
报错 java.lang.Exception: java.lang.ClassCastException: org.apache.hadoop.mapreduce.lib.input.FileSpli ...
- js 时间戳转时间格式
$("#showbidMessage").append(<span>' + ChangeDateFormat(rows[i].createTime) + '</s ...
- 智能合约 solidity 开发的环境基本搭建
以太坊Dapp开发快速入门 以太坊为开源社区,虽然设计东西都很优秀,但是组件十分的杂乱,因此下面首先简单介绍下以太坊的一些常用组件以及各种工具介绍 Geth Geth是由以太坊基金会提供的官方客户端软 ...
- 什么是JDK?什么是JRE?JDK与JRE的区别和用途
一.编程环境与运行环境 JDK(Java Development Kit)称为Java开发包或Java开发工具.是一个编写Java的Applet小程序和应用程序的程序开发环境.JDK是整个Java的核 ...
- pom文件
groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找. groupId一般分为多个段 ...
- Ubuntu 16.04安装JDK(转载)
1.简单的安装方法 安装JDK的最简单方法应该就是使用apt-get来安装了,但是源一般是OpenJDK,如果需要安装Oracle的JDK这种方法就不合适了,直接跳过看下面的章节. 1.使用ctrl+ ...
- LeetCode算法题-Reverse Linked List(Java实现)
这是悦乐书的第192次更新,第195篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第51题(顺位题号是206).反转单链表.例如: 输入:1-> 2-> 3- ...
- 【工具大道】UML的点点滴滴
本文地址 点击关注微信公众号 wenyuqinghuai 分享提纲: 1. 概述 2. UML类图 3. UML时序图 4. 参考资料 1.概述 1.1)百度百科: 又称统 ...
- 【Java多线程通信】syncrhoized下wait()/notify()与ReentrantLock下condition的用法比较
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6556925.html 一:syncrhoized使用同一把锁的多个线程用通信实现执行顺序的调度 我们知道,使 ...
- 让你提前认识软件开发(21):C程序中的定时器
版权声明:本文为博主原创文章.对文章内容有不论什么意见或建议.欢迎与作者单独交流.作者QQ(微信):245924426. https://blog.csdn.net/zhouzxi/article/d ...