题面
Consider an grid filled with numbers as follows:
- the first row contains integers from to from left to right;
- the second row contains integers from to from left to right;
- this pattern continues until the -th row, which contains integers from to 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 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 .
Input
The first line contains a single integer () — the number of test cases.
The only line of each test case contains a single integer ().
Output
For each test case, print a single integer — the maximum cost among all cells in the grid.
思路
设位置为第 行第 列则值为
相邻格共享边因此最多 个邻居且每个邻居与自身相差 或
最大值一定出现在靠右下的位置因为邻居与自身都更大
- 当 可直接得答案为 以及
- 当 只需比较右下角 内的候选
令 则其代价为
令 则其代价为
其余两格的邻居数更少且代价更小因此最大值为
比较差值
因此 时答案为 且 时答案为
代码
#include <bits/stdc++.h>
using namespace std;
int main() { int t; cin >> t; while (t --) { long long n; cin >> n;
if (n == 1) cout << 1 << endl; else if (n == 2) cout << 9 << endl; else if (n == 3 || n == 4) cout << 4*pow(n, 2) - n - 4 << endl; else cout << 5*pow(n, 2) - 5 * n - 5 << endl; }}