CF2170A.Maximum Neighborhood
入门
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Consider an n×n grid filled with numbers as follows:
- the first row contains integers from 1 to n from left to right;
- the second row contains integers from (n+1) to 2n from left to right;
- this pattern continues until the n-th row, which contains integers from (n2−n+1) to n2 from left to right.
Let's define the cost of a cell as its value plus the sum of its neighboring cells' values. Two cells are considered neighboring if they share a side.
Your task is to calculate the maximum cost among all cells in the grid.
The grid for n=4 and the optimal answer for it. The yellow cell has the maximum possible cost; the green cells are its neighbors. The cost of the cell is 15+11+14+16=56.
考虑一个 n×n 的网格,其中填入的数字如下:
- 第一行从左到右依次为整数 1 到 n;
- 第二行从左到右依次为整数 (n+1) 到 2n;
- 依此类推,直至第 n 行,该行从左到右依次为整数 (n2−n+1) 到 n2。
我们定义一个单元格的代价为其自身数值加上其所有相邻单元格数值之和。若两个单元格共享一条边,则称它们为相邻。
你的任务是计算该网格中所有单元格的最大代价。
当 n=4 时的网格及其最优解。黄色单元格具有可能的最大代价;绿色单元格是它的相邻单元格。该单元格的代价为 15+11+14+16=56。
输入格式
The first line contains a single integer t (1≤t≤100) — the number of test cases.
The only line of each test case contains a single integer n (1≤n≤100).
第一行包含一个整数 t(1≤t≤100)—— 测试用例的数量。
每个测试用例仅有一行,包含一个整数 n(1≤n≤100)。
输出格式
For each test case, print a single integer — the maximum cost among all cells in the grid.
对于每个测试用例,输出一个整数——网格中所有单元格的最大代价。
输入输出样例
输入#1
5 1 2 3 4 5
输出#1
1 9 29 56 95
说明/提示
In the first example, there is only 1 cell with the cost 1.
In the second example, the cell with value 4 has the maximum cost: 4+2+3=9.
In the third example, the cell with value 8 has the maximum cost: 8+5+7+9=29.
In the fourth example, the cell with value 15 has the maximum cost: 15+11+14+16=56.
In the fifth example, the cell with value 19 has the maximum cost: 19+14+18+20+24=95.
在第一个例子中,只有一个单元格的成本为 1。
在第二个例子中,值为 4 的单元格具有最大成本:4+2+3=9。
在第三个例子中,值为 8 的单元格具有最大成本:8+5+7+9=29。
在第四个例子中,值为 15 的单元格具有最大成本:15+11+14+16=56。
在第五个例子中,值为 19 的单元格具有最大成本:19+14+18+20+24=95。
输入解题思路,AI测评打分。不知道怎么写?