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。同理可证明不存在更优的方法。

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. ios::sync_with_stdio(false);
  8. int t;
  9. cin >> t;
  10. while (t--)
  11. {
  12. int h, w;
  13. cin >> h >> w;
  14.  
  15. if (h % && w % )
  16. {
  17. for (int i = ; i < h; i++)
  18. {
  19. for (int j = ; j < w; j++)
  20. cout << "(";
  21. cout << endl;
  22. }
  23. }
  24. else if (!(h % ) && w % )
  25. {
  26. for (int i = ; i < h; i++)
  27. {
  28. for (int j = ; j < w; j++)
  29. {
  30. if (i % )
  31. cout << ")";
  32. else
  33. cout << "(";
  34. }
  35. cout << endl;
  36. }
  37. }
  38. else if (!(w % ) && h % )
  39. {
  40. for (int i = ; i < h; i++)
  41. {
  42. for (int j = ; j < w; j++)
  43. {
  44. if (j % )
  45. cout << ")";
  46. else
  47. cout << "(";
  48. }
  49. cout << endl;
  50. }
  51. }
  52. else if (w == || h == )
  53. {
  54. if (w >= h)
  55. {
  56. for (int j = ; j < w; j++)
  57. cout << "(";
  58. cout << endl;
  59. for (int j = ; j < w; j++)
  60. cout << ")";
  61. cout << endl;
  62. }
  63. else
  64. {
  65. for (int i = ; i < h; i++)
  66. cout << "()" << endl;
  67. }
  68. }
  69. else if (w == || h == )
  70. {
  71. if (w >= h)
  72. {
  73. for (int i = ; i < h; i++)
  74. {
  75. for (int j = ; j < w; j++)
  76. {
  77. if (!i)
  78. cout << "(";
  79. else if (i == )
  80. {
  81. if (j < w / )
  82. cout << "(";
  83. else
  84. cout << ")";
  85. }
  86. else if (i == )
  87. {
  88. if (j >= w / )
  89. cout << "(";
  90. else
  91. cout << ")";
  92. }
  93. else
  94. cout << ")";
  95. }
  96. cout << endl;
  97. }
  98. }
  99. else
  100. {
  101. for (int i = ; i < h; i++)
  102. {
  103. for (int j = ; j < w; j += )
  104. {
  105. if (i < h / )
  106. cout << "()";
  107. else
  108. {
  109. if (j < w / )
  110. cout << "((";
  111. else
  112. cout << "))";
  113. }
  114. }
  115. cout << endl;
  116. }
  117. }
  118. }
  119. else
  120. {
  121. for (int i = ; i < h; i++)
  122. {
  123. for (int j = ; j < w; j++)
  124. {
  125. if (!i)
  126. cout << "(";
  127. else if (i == h - )
  128. cout << ")";
  129. else if (i % == )
  130. {
  131. cout << "()";
  132. j++;
  133. }
  134. else if (i % )
  135. {
  136. if (!j)
  137. cout << "(";
  138. else if (j == w - )
  139. cout << ")";
  140. else
  141. {
  142. cout << "()";
  143. j++;
  144. }
  145. }
  146. }
  147. cout << endl;
  148. }
  149. }
  150. }
  151.  
  152. return ;
  153. }

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. java算法 第七届 蓝桥杯B组(题+答案) 1.煤球数目

    1.煤球数目  (结果填空) 有一堆煤球,堆成三角棱锥形.具体:第一层放1个,第二层3个(排列成三角形),第三层6个(排列成三角形),第四层10个(排列成三角形),....如果一共有100层,共有多少 ...

  2. Quartz_2_简单编程式任务调度使用(CronTrigger)

    第二个要介绍的任务调度器中的触发器是 CronTrigger ,相比较 SimpleTrigger 来说,CronTrigger 相对灵活,对于复杂的业务需求来说,更加的实用.要在使用 CronTri ...

  3. sql设置字段默认值

    alter table 表名 modify 字段名 default 默认值;

  4. Codeforces 721E DP

    大概思路及题意看这篇博客吧 我的理解:设f[i]表示处理到第i个区间,能唱的最多的歌,g[i]是保证f[i]最大时最靠左的点.那么f[i] = max(f[j] + (r[i] - max(l[i], ...

  5. Jenkins 更新最新版本

    一般情况下,war的安装路径在/usr/share/jenkins目录下. 不过也有部分人不喜欢安装在这里,可以通过系统管理(System management)--> 系统信息(System ...

  6. 存储过程中使用事务和try catch

    一.存储过程中使用事务的简单语法 在存储过程中使用事务时非常重要的,使用数据可以保持数据的关联完整性,在Sql server存储过程中使用事务也很简单,用一个例子来说明它的语法格式: 代码 : Cre ...

  7. Mysql设置auto_increment_increment和auto_increment_offset

    查看与设置: show variables like '%auto_inc%'; show session variables like '%auto_inc%'; -- //session会话变量 ...

  8. Opencv轮廓计数(学习)

    #include <iostream>#include <opencv2/opencv.hpp>#include <opencv2/xfeatures2d.hpp> ...

  9. jdk+Tomcat环境

    1.Tomcat概述 Tomcat服务器由Apache提供,开源免费.安装Tomcat之前需要先安装JDK,其实无论哪一种Javaweb服务器都需要先安装JDK. Tomcat6支持Servlet2. ...

  10. dll函数生成规则

    [转]http://blog.csdn.net/beanjoy/article/details/9136127 所谓名字修饰约定,就是指变量名.函数名等经过编译后重新输出名称的规则. 比如源代码中函数 ...