欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF2057A.MEX Table
MEX Table
One day, the schoolboy Mark misbehaved, so the teacher Sasha called him to the whiteboard.
Sasha gave Mark a table with rows and columns. His task is to arrange the numbers in the table (each number must be used exactly once) in such a way as to maximize the sum of MEX across all rows and columns. More formally, he needs to maximize $$ um\limits_{i = 1}^{n} \operatorname{mex}({a_{i,1}, a_{i,2}, \ldots, a_{i,m}}) + um\limits_{j = 1}^{m} \operatorname{mex}({a_{1,j}, a_{2,j}, \ldots, a_{n,j}}),$$whereis the number in the-th row and-th column.
Sasha is not interested in how Mark arranges the numbers, so he only asks him to state one number — the maximum sum of MEX across all rows and columns that can be achieved.
The minimum excluded (MEX) of a collection of integersis defined as the smallest non-negative integerwhich does not occur in the collection.
For example:
- , sincedoes not belong to the array.
- , sinceandbelong to the array, butdoes not.
- , since,,, andbelong to the array, but does not.</p>
Input
Each test contains multiple test cases. The first line contains a single integer () — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers and () — the number of rows and columns in the table, respectively.
Output
For each test case, output the maximum possible sum of across all rows and columns.
Note
In the first test case, the only element is , and the sum of the of the numbers in the first row and the of the numbers in the first column is $\operatorname{mex}(\{0\}) + \operatorname{mex}(\{0\}) = 1 + 1 = 2$.
In the second test case, the optimal table may look as follows:
Then $um\limits_{i = 1}^{n} \operatorname{mex}(\{a_{i,1}, a_{i,2}, \ldots, a_{i,m}\}) + um\limits_{j = 1}^{m} \operatorname{mex}(\{a_{1,j}, a_{2,j}, \ldots, a_{n,j}\}) = \operatorname{mex}(\{3, 0\}) + \operatorname{mex}(\{2, 1\})$ $+ \operatorname{mex}(\{3, 2\}) + \operatorname{mex}(\{0, 1\}) = 1 + 0 + 0 + 2 = 3$.
Samples
3 1 1 2 2 3 52 3 6
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |