题目链接

Problem Description
A parentheses matrix is a matrix where every element is either '(' or ')'. We define the goodness of a parentheses matrix as the number of balanced rows (from left to right) and columns (from up to down). Note that:

- an empty sequence is balanced;
- if A is balanced, then (A) is also balanced;
- if A and B are balanced, then AB is also balanced.

For example, the following parentheses matrix is a 2×4 matrix with goodness 3, because the second row, the second column and the fourth column are balanced:

)()(
()()

Now, give you the width and the height of the matrix, please construct a parentheses matrix with maximum goodness.

Input
The first line of input is a single integer T (1≤T≤50), the number of test cases.

Each test case is a single line of two integers h,w (1≤h,w≤200), the height and the width of the matrix, respectively.

Output
For each test case, display h lines, denoting the parentheses matrix you construct. Each line should contain exactly w characters, and each character should be either '(' or ')'. If multiple solutions exist, you may print any of them.
Sample Input
3
1 1
2 2
2 3
Sample Output
(
()
)(
(((
)))
题意
给一个只含'('和')'的矩阵,只考虑从行和列上的括号序列,构造一个矩阵使得合法括号序列的总数最多
分析
首先奇数行或奇数列内是不存在合法括号序列的,所以如果n或m是奇数,则最多有n/m个括号序列(即把行/列直接填充为合法序列),需要分析的是偶数行和偶数列的情况。
首先贪心一下,起点位于第一行和第一列,所以应该尽量在这些位置填'(',首先想到的是把矩形的左上边界填充为'(',右下边界填充为')'
因为第一行,第n行,第1列,第m列一定不是序列,所以这样最多有n+m-4个合法括号序列。

但有一个情况比较特殊,当n=4的时候,上面的方法其实比较亏,以牺牲第一列和最后一列的代价,却只得到了两行合法括号序列。考虑另外一种填充方法:当n比较小的时候,把第一行全部填充为'(',最后一行全部填充为')'

这样以后发现,可以通过调整剩下的位置,让剩下一半的行数成为合法的序列,于是这样最多有(n-2)/2+m=n/2-1+m个合法括号序列
比较一下上面两种方案,因为n和m是可以互换的,不妨假设m>n,第一种方案最多有m+n-4个合法序列,第二种方案最多应该有m+n/2-1,当他们相等时,m+n-4=m+n/2-1,解得n=6,也就是n,m较小的那个比6小的时候,采用第二种方案可以获得更多序列,而n,m都大于等于6的时候应该选择第一种情况。
代码
#include<stdio.h>

char w[][];
#define min(a,b) ((a)<(b)?(a):(b))
int main(){
int kase;
int n,m;
scanf("%d",&kase);
while(kase--) {
scanf("%d %d",&n,&m);
if((n&)&&(m&)){/*奇数行 奇数列 0个*/
for(int i=;i<n;++i)
for(int j=;j<m;++j)w[i][j]='('; }
else if(n&){/*奇数行 偶数列 n个*/
for(int i=;i<n;++i){
w[i][]='(';
for(int j=;j<m;++j)
w[i][j]='('+')'-w[i][j-];
}
}
else if(m&){/*偶数行 奇数列 m个*/
for(int j=;j<m;++j){
w[][j]='(';
for(int i=;i<n;++i)
w[i][j]='('+')'-w[i-][j];
}
}
else {/*偶数行 偶数列*/
if(min(n,m)<=){/*选择方案2*/
if(n>m){//行多,n+m/2-1个
for(int i=;i<n;++i)w[i][]='(';
for(int j=;j<m-;++j){
w[][j]='('+')'-w[][j-];
for(int i=;i<n;++i)w[i][j]='('+')'-w[i-][j];
}
for(int i=;i<n;++i)w[i][m-]=')';
}
else {//列多,m+n/2-1个
for(int j=;j<m;++j)w[][j]='(';
for(int i=;i<n-;++i){
w[i][]='('+')'-w[i-][];
for(int j=;j<m;++j)w[i][j]='('+')'-w[i][j-];
}
for(int j=;j<m;++j)w[n-][j]=')';
}
}
else {//偶数行,偶数列 列+行-4个
w[][]='(';w[][m-]=')';
w[n-][]='(';w[n-][m-]=')';
for(int j=;j<m-;++j){//
w[][j]='(';w[n-][j]=')';
}
for(int i=;i<n-;++i){
w[i][]='(';w[i][m-]=')';
}
for(int i=;i<n-;++i){
w[i][]='('+')'-w[i-][];
for(int j=;j<m-;++j){
w[i][j]='('+')'-w[i][j-];
}
}
}
}
/*输出*/
for(int i=;i<n;++i){
for(int j=;j<m;++j)
printf("%c",w[i][j]);
printf("\n");
}
}
}
总结
一旦某一行是合法序列,那么它一定一半的位置是'(',另一半是')',如果让合法序列出现在首行/列,末行/列,那么一半的列/行都不会是合法序列了,所以这些位置一定不要出现合法序列,那就尽量贪心,尽量填充为全'('或')'。
但是,当行/列数较小的时候,牺牲一半的行/列不一定是坏事,应该特判一下

hdu 6400 Parentheses Matrix的更多相关文章

  1. HDU - 6400 多校8 Parentheses Matrix(构造)

    Parentheses Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  2. hdu 4965 Fast Matrix Calculation(矩阵高速幂)

    题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...

  3. HDU 4965 Fast Matrix Calculation(矩阵高速幂)

    HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...

  4. hdu多校第八场Parentheses Matrix

    #include<bits/stdc++.h> using namespace std; ][]; int main() { int t; scanf("%d",&am ...

  5. 矩阵乘法 --- hdu 4920 : Matrix multiplication

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  6. hdu 4965 Fast Matrix Calculation

    题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...

  7. hdu 5015 233 Matrix(构造矩阵)

    http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...

  8. HDU 3666.THE MATRIX PROBLEM 差分约束系统

    THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. HDU - 4965 Fast Matrix Calculation 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4965 题意 给出两个矩阵 一个A: n * k 一个B: k * n C = A * B M = (A ...

随机推荐

  1. 20155316 2016-2017-2《Java程序设计》课程总结

    每周作业 链接汇总 预备作业1:学习调查(专业期望 师生关系 代码行数) 预备作业2:"做中学"调查(日常技能 C语言 Java 公文写作) 预备作业3:Linux系统与虚拟机学习 ...

  2. 好玩的虚拟机和有趣的Linux系统 ——20155332

    color=#安装虚拟机 学习基于VirtualBox虚拟机安装Ubuntu图文教程在自己笔记本上安装Linux操作系统 我从官网下载了Ubuntu64位操作系统和VMare Workstion虚拟机 ...

  3. 你真的认为iphone只是一部手机么

    闲言不表,直奔主题.我是一个程序员,上周参加了一个开源软件交流大会,其实会上并没有听到什么新鲜的东西.但是在会中,偶然间听到了一个关于iphone的秘密,却着实令我震惊了,事情具体是这样的,听我慢慢道 ...

  4. day 3 局部变量 全局变量

    1.局部变量 2.全局变量(死歌的大招)函数前面声明的都是全局变量 3.全局变量和局部变量的区别 1)老方法 def get_temper(): temper = 33 return temper d ...

  5. Zabbix学习之路(一)之Zabbix安装

    一.Zabbix环境准备 [root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@linux-n ...

  6. 写一个 setter 方法用于完成 @property (nonatomic, retain) NSString *name,

    写一个 setter 方法用于完成 @property (nonatomic, retain) NSString *name 写一个 setter 方法用于完成 @property (nonatomi ...

  7. 【MYSQL经验】MYSQL经验总结

    1.决定是否添加一个新的所以并部署它需要考虑很多因素

  8. 谈谈你对Java异常处理机制的理解

    先谈谈我的理解:异常处理机制可以说是让我们编写的程序运行起来更加的健壮,无论是在程序调试.运行期间发生的异常情况的捕获,都提供的有效的补救动作,任何业务逻辑都会存在异常情况,这时只需要记录这些异常情况 ...

  9. Spring Boot 整合JDBC 实现后端项目开发

    一.前言 二.新建Spring Boot 项目 三.Spring Boot 整合JDBC 与MySQL 交互 3.1 新建数据表skr_user 3.2 Jdbcproject 项目结构如下 3.3 ...

  10. JAVA学习笔记--正则表达式

    正则表达式是一种强大而灵活的文本处理工具.使用正则表达式,可以让我们以编程的方式构造复杂的文本,并对输入的字符串进行搜索. 一.基础正则表达式语法(表格来自J2SE6_API) 字符 x 字符 x \ ...