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. kubernetes ServiceAccount

    api server 启动参数增加 –admission_control=…,ServiceAccount,…

  2. 使用vue-cli创建一个vue项目

    安装vue-cli npm install -g @vue/cli 1, 使用vue创建一个项目 vue create luffy 2, 安装所需的插件 npm install vue-router ...

  3. Java-精确计算工具类

    import java.math.BigDecimal; import java.math.RoundingMode; /** * 精确计算工具类(加,减,乘,除,返回较大值,返回较小值) */ pu ...

  4. ROS tf 编程指南

    ROS (Robot Operating System, 机器人操作系统)是最知名的机器人操作系统,广泛应用于无人驾驶和机器人,tf(transforms,坐标系转换)是ROS下的一个常用的工具库.r ...

  5. real-Time Correlative Scan Matching

    启发式算法(heuristic algorithm)是相对于最优化算法提出的.一个问题的最优算法求得该问题每个实例的最优解.启发式算法可以这样定义:一个基于直观或经验构造的算法,在可接受的花费(指计算 ...

  6. 彻底修改Eclipse的默认编码

    引用各位前辈经验得到彻底修改eclipse默认编码的方法. 单在eclipse里设置编码方式非常复杂且容易遗漏,全部修改后,有些代码生成模板内的${encode}变量仍为原编码方案,经过查阅许多资料得 ...

  7. 4.4.3 Java中的指针:Unsafe类

    java多线程编程的无锁CAS底层都是通过 Unsafe进行操作的:源码如下 public final boolean compareAndSet(int expect, int update) { ...

  8. [GO]方法的继承

    package main import "fmt" type Person struct { name string sex byte age int } func (tmp Pe ...

  9. QtCreator下QML翻译

    首先打开.pro工程文件,在文件中添加文本段:TRANSLATIONS = testTranslate_zh.ts 在pro右键,单击再次弹出命令提示,如图 在命令行中,输入lupdate testT ...

  10. Paint的ColorFilter

    一.简介 setColorFilter(ColorFilter filter) 设置颜色过滤,这个方法需要我们传入一个ColorFilter参数同样也会返回一个ColorFilter实例.我们在set ...