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 ...
随机推荐
- 【MM系列】SAP库龄报表逻辑理解
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP库龄报表逻辑理解 第一篇 ...
- c/c++ 标准库 插入迭代器 详解
标准库 插入迭代器 详解 插入迭代器作用:copy等函数不能改变容器的大小,所以有时copy先容器是个空的容器,如果不使用插入迭代器,是无法使用copy等函数的. 例如下面的代码就是错误的: list ...
- 编写脚本实现DHCP服务与DHCP中继自动化执行
编写脚本实现DHCP服务与DHCP中继自动化执行 本脚本是在liunx搭建DHCP服务器以及DHCP中继服务器实验环境下实现的https://www.cnblogs.com/yuzly/p/10539 ...
- [Hive_3] Hive 建表指定分隔符
0. 说明 Hive 建表示例及指定分隔符 1. Hive 建表 Demo 在 Hive 中输入以下命令创建表 user2 create table users2 (id int, name stri ...
- 十大经典排序算法的python实现
十种常见排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序.包括:冒泡排序.选择排序.归并排序.快速 ...
- Glyphicons 字体图标
- Java-Socket实现文件的断点续传
前段时间因为任务需要本人这个java渣渣开始研究如何用java实现简单的文件断点续传.所谓的文件断点续传,我的理解是文件在传输过程中因为某些原因程序停止运行文件终止传输,下一次重新传输文件的时候还能从 ...
- wangedit
<template> <el-row id="AddRoom"> <el-col :span="5">.</el-co ...
- 部署与管理ZooKeeper(版本有点老,3.4.3)
本文以ZooKeeper3.4.3版本的官方指南为基础:http://zookeeper.apache.org/doc/r3.4.3/zookeeperAdmin.html,补充一些作者运维实践中的要 ...
- 退出全屏监听ESC事件,这里没有用keydown来监听,因为全屏时候keydown监听不到
浏览器故意不上监听ESC键的,跟网页加载完成之后不能用程序使浏览器全屏一样的道理,避免开发者恶意全屏,不啰嗦,解决办法如下:window.onresize = function(){if(!check ...