Parentheses Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 728    Accepted Submission(s): 294
Special Judge

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
(
()
)(
(((
)))
 

这题。。。。WA到死啊 在h=2那里摔倒了好几次都没看出来

题意是构造一个 h×w 的括号矩阵,并且使其匹配的行数、列数之和最大的矩阵之一。

当 h 和 w 中有一个是奇数时,构造的方法是显然的。下面仅讨论 h 和 w 都是偶数的情况。不妨设 h≤w。 首先,第一行、最后一列中最多只有一个能够匹配,第一列、最后一行也只有一个能够匹配(考虑右上角和左下角的括号选取方法),故答案不会超过 w+h−2。

当 h = 2 时,每一列可以构造一对匹配,这样答案已经到达上界。 当 h = 4 时,可以如下构造:

((((((

)))(((

((()))

))))))

这样答案是 w+h−3。若存在更优的答案,则必有一个边界能够匹配,不妨设是第一列。这样,就已 经有除第一行以外的两行(右括号开头的行)不匹配了,而第一行和最后一列中至少有一个不匹配, 因此最优解不会超过 w+h−3。

当 h≥6 时,可以如下构造:

((((((((

()()()()

(()()())

()()()()

(()()())

))))))))
答案是 w+h−4。同理可证明不存在更优的方法。

 #include <iostream>

 using namespace std;

 int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
int h, w;
cin >> h >> w; if (h % && w % )
{
for (int i = ; i < h; i++)
{
for (int j = ; j < w; j++)
cout << "(";
cout << endl;
}
}
else if (!(h % ) && w % )
{
for (int i = ; i < h; i++)
{
for (int j = ; j < w; j++)
{
if (i % )
cout << ")";
else
cout << "(";
}
cout << endl;
}
}
else if (!(w % ) && h % )
{
for (int i = ; i < h; i++)
{
for (int j = ; j < w; j++)
{
if (j % )
cout << ")";
else
cout << "(";
}
cout << endl;
}
}
else if (w == || h == )
{
if (w >= h)
{
for (int j = ; j < w; j++)
cout << "(";
cout << endl;
for (int j = ; j < w; j++)
cout << ")";
cout << endl;
}
else
{
for (int i = ; i < h; i++)
cout << "()" << endl;
}
}
else if (w == || h == )
{
if (w >= h)
{
for (int i = ; i < h; i++)
{
for (int j = ; j < w; j++)
{
if (!i)
cout << "(";
else if (i == )
{
if (j < w / )
cout << "(";
else
cout << ")";
}
else if (i == )
{
if (j >= w / )
cout << "(";
else
cout << ")";
}
else
cout << ")";
}
cout << endl;
}
}
else
{
for (int i = ; i < h; i++)
{
for (int j = ; j < w; j += )
{
if (i < h / )
cout << "()";
else
{
if (j < w / )
cout << "((";
else
cout << "))";
}
}
cout << endl;
}
}
}
else
{
for (int i = ; i < h; i++)
{
for (int j = ; j < w; j++)
{
if (!i)
cout << "(";
else if (i == h - )
cout << ")";
else if (i % == )
{
cout << "()";
j++;
}
else if (i % )
{
if (!j)
cout << "(";
else if (j == w - )
cout << ")";
else
{
cout << "()";
j++;
}
}
}
cout << endl;
}
}
} return ;
}

HDU6400-2018ACM暑假多校联合训练1004-Parentheses Matrix-构造的更多相关文章

  1. HDU6336-2018ACM暑假多校联合训练4-1005-Problem E. Matrix from Arrays-前缀和

    题意是给了一种矩阵的生成方式 让你求两个左边之间的矩阵里面的数加起来的和(不是求矩阵的值) 没看标程之前硬撸写了160行 用了前缀和以后代码量缩短到原来的1/3 根据规律可以推导出这个矩阵是在不断重复 ...

  2. HDU6333-2018ACM暑假多校联合训练1002-Harvest of Apples-莫队+费马小定理

    题意很简单啦,求S(n,m)的值 通过打表我们可以知道 S(n + 1, m) = S(n, m) * 2 - C(n, m); S(n - 1, m) = (S(n, m) + C(n - 1, m ...

  3. HDU6342-2018ACM暑假多校联合训练4-1011-Problem K. Expression in Memories

    Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262 ...

  4. HDU6330-2018ACM暑假多校联合训练Problem L. Visual Cube

    就是画个图啦 分三个平面去画orz #include <iostream> #include <cmath> #include <cstring> #include ...

  5. HDU6318-2018ACM暑假多校联合训练2-1010-Swaps and Inversions-树状数组

    本题题意是,给你一个长度为n的序列,使用最少的操作把序列转换为从小到大的顺序,并输出操作数*min(x,y) 实质上是算出该序列中有多少逆序对,有归并排序和树状数组两种算法,由于数据之间的差值有点大, ...

  6. HDU6299-2018ACM暑假多校联合训练1002-Balanced Sequence

    这个题的题意是给你n个字符串,认定()是一种平衡的串,两个以上连续的()()也是一种平衡的串,如果一对括号里面包含一个平衡的串,这个括号也被算在这个平衡的串之内, 如(()(()))是一个长度为8的平 ...

  7. HDU6298-2018ACM暑假多校联合训练1001-Maximum Multiple

    题意大致是给你一个整数n,让你确定是否有三个正整数x,y,z既能被n整除,又能x+y+z=n,并使xyz最大 从中根据规律可以看出,只有被3或被4整除的数才能满足题目要求 被3整除的最大值为n^3/3 ...

  8. HDU6301-2018ACM暑假多校联合训练1004-Distinct Values

    题意是一个长度为n的序列,给你m组区间(l,r),在这个区间里不能填入重复的数字,同时使整个序列字典序最小 同学用的优先队列,标程里使用的是贪心同时使用set维护答案序列 贪心是先采用pre数组来确定 ...

  9. HDU6308-2018ACM暑假多校联合训练1011-Time Zone

    题目大意就是给你UTC-8时区的时间 让你求对应时区的时间 哇 这个题 看似简单,但是一开始怎么都过不了啊 同学用自己写的read过了,后来看了一下各位大佬说改成分钟随便过,就随便过了 Problem ...

随机推荐

  1. Android开发实战之拥有Material Design风格的折叠布局

    关于折叠布局,也许你并不陌生,最新版的陌陌,或者一些其他的社交APP都有一个折叠布局.折叠布局,让我们的APP更加具有交互性,同时也更加美观,先来展示一下效果图: 这是我个人做的一个APP主界面,可以 ...

  2. css3中的transform、transition、translate、animation(@keyframes)的区别

    一.前言 在CSS中,我们经常会使用到transform.transition.translate.animation(@keyframes)这些长得相似,又不好区分的属性(值).每当需要使用它们,都 ...

  3. 使用zTree展开节点后,覆盖了下一个节点

    如图所示,结果是zTree与<fieldset>标签不兼容....我去!!! 也就是说Ztree不能放在<fieldset>标签中..

  4. 安装操作系统CentOS-7.x

    一.创建虚拟机 使用VMware Fusion创建虚拟机 二.系统安装 为了统一环境,保证实验的通用性,将网卡名称设置为eth*,不使用CentOS 7默认的网卡命名规则.所以需要在安装的时候,增加内 ...

  5. cactiez中文版10.1配置监控系统安装笔记

    1.安装虚拟机vmware_player2.创建虚拟机,设置桥接模式,内存4g,磁盘大小50G3.启动虚拟机,安装系统4.系统root 默认密码 CactiEZ5.配置网络静态IP,修改IP,网关等信 ...

  6. TF录像存储专项测试

    测试环境 移动设备:小米4C 移动设备版本:Android 5.1 IPC版本号:0.1.4110_10.1.1.1.3948 安居小宝版本:Version:2.0.1 测试网络:IPC使用WIFI网 ...

  7. 1097G Vladislav and a Great Legend

    传送门 分析 https://blog.csdn.net/forever_shi/article/details/88048528 代码 #include<iostream> #inclu ...

  8. 无废话 Thrift 之 Hello World( PHP 版).

    Thrift 不再介绍.体验一把 PHP 的Server , PHP 的Client. 0.安装,装备环境,不表,运行 thrift -version 看到版本就行. 1.写 HelloThrift. ...

  9. 并发编程CAS操作

    并发编程CAS操作 简介 CAS即compare and swap,中文就是比较并交换 CAS是Java并发包的基石 原理 其实CAS的原理相对来说比较简单.将要被改变的数据和期望的值作比较,当两个值 ...

  10. css3的那些高级选择器二

    在上个星期我介绍了css3的属性选择器,伪类选择器和结构伪类选择器,今天楼主继续把其它的css3选择器说完. 在css3中,共有11中UI状态伪类选择器,分别是E:hover,E:active,E:f ...