CONTINUE...?【构造/分析】
CONTINUE...?
Time Limit: 1 Second Memory Limit: 65536 KB Special Judge
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
【题意】:https://www.cnblogs.com/bluefly-hrbust/p/8971769.html
本题题意就是把1到n数表示为,两组数之和相等,即能不能1->n划分成两部分(男女分组是干扰的)
1 2 3 4 5 6 7 8
偶数-->1+8+2+7 = 3+6+4+5
1 2 3 4 5 6 7
奇数-->1+3+4+6 = 2+5+7
偶数: n只需要前后匹配即可
奇数: n/2之前的奇数位和n/2之后的偶数位相加,等于n/2之前的偶数位加n/2的奇数位
【分析】:ACZone+
【出处】:CodeForces - 899C Dividing the numbers
【代码】:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int t,n,len;
char a[100050];
int b[100050];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
getchar();
scanf("%s",&a);
len=strlen(a);
if ((n%4)<=2 && n%4!=0)
{
printf("-1\n");
}
else
{
if(n%2==0) //偶数
{
for(int i=0; i<n/4; i++)
{
if (a[i]=='1')printf("3");
else printf("1");
}
for (int i=n/4; i<n-n/4; i++)
{
if (a[i]=='1')printf("4");
else printf("2");
}
for (int i=n-n/4; i<n; i++)
{
if (a[i]=='1')printf("3");
else printf("1");
}
printf("\n");
}
else //奇数
{
for(int i=0; i<n/2; i++)
{
if (a[i]=='1')
{
if ((i+1)%2==1)printf("3");
else printf("4");
}
else
{
if ((i+1)%2==1)printf("1");
else printf("2");
}
}
for(int i=n/2; i<len; i++)
{
if (a[i]=='1')
{
if ((i+1)%2==1)printf("4");
else printf("3");
}
else
{
if ((i+1)%2==1)printf("2");
else printf("1");
}
}
printf("\n");
}
}
}
return 0;
}
CONTINUE...?【构造/分析】的更多相关文章
- 任意文件下载漏洞的接口URL构造分析与讨论
文件下载接口的URL构造分析与讨论 某学院的文件下载接口 http://www.****.edu.cn/item/filedown.asp?id=76749&Ext=rar&fname ...
- popchain与对应poc的构造分析
本文首发于:https://mp.weixin.qq.com/s?__biz=MjM5MTYxNjQxOA==&mid=2652850238&idx=1&sn=6f22d8ab ...
- 《编译原理》构造 LL(1) 分析表的步骤 - 例题解析
<编译原理>构造 LL(1) 分析表的步骤 - 例题解析 易错点及扩展: 1.求每个产生式的 SELECT 集 2.注意区分是对谁 FIRST 集 FOLLOW 集 3.开始符号的 FOL ...
- 编译原理根据项目集规范族构造LR(0)分析表
转载于https://blog.csdn.net/Johan_Joe_King/article/details/79058597?utm_medium=distribute.pc_relevant.n ...
- 基于虎书实现LALR(1)分析并生成GLSL编译器前端代码(C#)
基于虎书实现LALR(1)分析并生成GLSL编译器前端代码(C#) 为了完美解析GLSL源码,获取其中的信息(都有哪些in/out/uniform等),我决定做个GLSL编译器的前端(以后简称编译器或 ...
- 【编译原理】语法分析LL(1)分析法的FIRST和FOLLOW集
近来复习编译原理,语法分析中的自上而下LL(1)分析法,需要构造求出一个文法的FIRST和FOLLOW集,然后构造分析表,利用分析表+一个栈来做自上而下的语法分析(递归下降/预测分析),可是这个FIR ...
- 【集合框架】JDK1.8源码分析之ArrayList详解(一)
[集合框架]JDK1.8源码分析之ArrayList详解(一) 一. 从ArrayList字表面推测 ArrayList类的命名是由Array和List单词组合而成,Array的中文意思是数组,Lis ...
- 面向对象软件构造 (Bertrand Meyer 著)
Part A: The Issues 议题 第一章 软件品质 第二章 面向对象的标准 Part B: The Road To Object Orientation 通向面向对象之路 第三章 模块性 第 ...
- 编译原理--02 自顶向下、自底向上的LR分析复习(清华大学出版社第3版)
前言 目录 01 文法和语言.词法分析复习 02 自顶向下.自底向上的LR分析复习 03 语法制导翻译和中间代码生成复习 04 符号表.运行时存储组织和代码优化复习 第4章 自顶向下的语法分析方法 确 ...
随机推荐
- Erlang OTP学习:supervisor [转]
转自: http://diaocow.iteye.com/blog/1762895 今天细致的看了下supervisor,现在做个总结: 其中,方块代表supervisor process,它的功能很 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目9
2014-04-28 23:52 题目:设计算法,找出一本书中某个单词的出现频率. 解法:数就行了. 代码: // 17.9 Given a book, find out the occurrence ...
- 就算WORD高手也无法解释的Word的一些疑惑.,一些已经解决,一些没有解决
如下功能如何用? 1.选项->保存->显示其他保存位置(即使可能需要登录)? 解答:您能告诉我吗? 2.字体->为字体调整字间距? 解答:自动调整某些字符之前的距离,使得更加美观.例 ...
- 六 APPIUM Android 定位方式
文本转自:http://www.cnblogs.com/sundalian/p/5629500.html APPIUM Android 定位方式 1.定位元素应用元素 1.1通过id定位元素 An ...
- [转]unity之LOD
LOD技术有点类似于Mipmap技术,不同的是,LOD是对模型建立了一个模型金字塔,根据摄像机距离对象的远近,选择使用不同精度的模型. 它的好处是可以在适当的时候大量减少需要绘制的顶点数目. 它的缺点 ...
- NOIP2018 集训(二)
A题 神炎皇 问题描述 神炎皇乌利亚很喜欢数对,他想找到神奇的数对. 对于一个整数对 \((a,b)\) ,若满足 \(a+b\leq n\) 且 \(a+b\) 是 \(ab\) 的因子,则称 为神 ...
- Python全栈工程师(迭代器、字节串)
ParisGabriel 每天坚持手写 一天一篇 决定坚持几年 为了梦想为了信仰 Python人工智能从入门到精通 迭代器 Iterator: 用<&g ...
- 深入探讨ui框架
深入探讨前端UI框架 1 前言 先说说这篇文章的由来 最近看riot的源码,发现它很像angular的dirty check,每个component ( tag )都保存一个expressions数组 ...
- Extjs msgTarget 提示位置
extjs msgTarget 有效值包括: qtip:显示一个浮动的提示消息 title:显示一个浏览器浮动提示消息 under:在字段下面显示一个提示消息,使用under时要注意表单的高度 sid ...
- android DOM解析Xml
文章转自:http://blog.sina.com.cn/s/blog_a661f16c0101d5qp.html People类是自己写的一个类,主要保存各个字符串数据. 由于没学过Xml语法只 ...