CF1447B.Numbers Box
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a rectangular grid with n rows and m columns. The cell located on the i-th row from the top and the j-th column from the left has a value aij written in it.
You can perform the following operation any number of times (possibly zero):
- Choose any two adjacent cells and multiply the values in them by −1. Two cells are called adjacent if they share a side.
Note that you can use a cell more than once in different operations.
You are interested in X, the sum of all the numbers in the grid.
What is the maximum X you can achieve with these operations?
给你一个 n 行 m 列的矩形网格。位于从上往下第 i 行、从左往右第 j 列的格子中写有一个数值 aij。
你可以执行以下操作任意次(包括零次):
- 任选两个相邻的格子,并将其中的数值同时乘以 −1。若两个格子有一条公共边,则称它们相邻。
注意:同一个格子可在不同的操作中被多次使用。
你关心的是 X,即网格中所有数字的总和。
通过这些操作,你能得到的最大 X 是多少?
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). Description of the test cases follows.
The first line of each test case contains two integers n,m (2≤n, m≤10).
The following n lines contain m integers each, the j-th element in the i-th line is aij (−100≤aij≤100).
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤100)。随后是测试用例的描述。
每个测试用例的第一行包含两个整数 n、m(2≤n,m≤10)。
接下来的 n 行,每行包含 m 个整数;其中第 i 行的第 j 个元素为 aij(−100≤aij≤100)。
输出格式
For each testcase, print one integer X, the maximum possible sum of all the values in the grid after applying the operation as many times as you want.
对于每个测试用例,输出一个整数 X,即在任意多次执行该操作后,网格中所有数值之和的最大可能值。
输入输出样例
输入#1
2 2 2 -1 1 1 1 3 4 0 -1 -2 -3 -1 -2 -3 -4 -2 -3 -4 -5
输出#1
2 30
说明/提示
In the first test case, there will always be at least one −1, so the answer is 2.
In the second test case, we can use the operation six times to elements adjacent horizontally and get all numbers to be non-negative. So the answer is: 2×1+3×2+3×3+2×4+1×5=30.
在第一个测试用例中,始终至少存在一个 −1,因此答案为 2。
在第二个测试用例中,我们可对水平相邻的元素执行六次操作,使所有数字均变为非负数。因此答案为:2×1+3×2+3×3+2×4+1×5=30。
输入解题思路,AI测评打分。不知道怎么写?