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. Setup Apache2 in Debian 9 and enable two ports for two sites

    root@debian:~# apt-get install apache2 root@debian:~# cd /etc/apache2/ root@debian:/etc/apache2# ls ...

  2. 从零开始搭建k8s-20180301

    yum install -y yum-utils git etcd yum-config-manager --add-repo https://download.docker.com/linux/ce ...

  3. 用java实现一个简易编译器2-语法解析

  4. 【UVA11419 训练指南】我是SAM 【二分图最小覆盖,最小割】

    题意 给出一个R*C大小的网格,网格上面放了一些目标.可以在网格外发射子弹,子弹会沿着垂直或者水平方向飞行,并且打掉飞行路径上的所有目标.你的任务是计算最少需要多少子弹,各从哪些位置发射,才能把所有目 ...

  5. 解剖Nginx·自动脚本篇(6)编译器名称变量脚本 auto/cc/name

    回顾变量 CC 最初是在auto/options脚本中初始化的: CC=${CC:-gcc} 1 C Compiler 的 feature Windows 平台的编译器叫做MSVC,其他平台的都统称为 ...

  6. Codeforces 667C DP

    题意:给你一个字符串,这个字符串的构造方法如下:先选择一个长度大于4的前缀,然后每次向字符串尾部添加一个长度为2或者长度为3的后缀,不能添加连续的相同的后缀,问可能的后缀有哪些?并按字典序输出去. 思 ...

  7. spring4-2-bean配置-3-自动装配

  8. c# 类中使用ResolveUrl

    类中使用ResolveUrl 1>获取当前page然后调用ResolveUrl System.Web.UI.Page page = HttpContext.Current.CurrentHand ...

  9. linux进入单用户方法-乾颐堂

    RedHat7.2 在使用GRUB引导程序的时候如何进入单用户 1.在出现GURB引导画面时,按字母e,进入GRUB编辑状态 2在引导菜单后添加“1”或single,选定它,然后按字母b,就可引导到单 ...

  10. 案例研究:手机APP的UI设计流程

    以下内容由Mockplus(http://www.mockplus.cn)团队翻译整理,仅供学习交流. UI设计——不仅仅是创造漂亮的图像. 面临的挑战 我为自己提供了一个绝佳的机会来训练我的视觉设计 ...