题目
Mr. G invents a new game whose rules are given as follows.
Firstly, he has a n×n matrix, all elements of which are 0 initially. Then, he follows up with some operations: in each time he chooses a row or a column, and adds an arbitrary positive integer to all the elements in the selected row or column. When all operations have been finished, he hides an element in the matrix and the element is modified to −1.
Now given the final matrix, you are asked to find out what the hidden element should be before the very last hiding operation.
Input
The first line contains a single integer n (2≤n≤1000).
Next n lines represent the matrix after the operations. Each element in the matrix satisfies −1≤ai,j≤1000000, and exactly one element is −1.
Output
Output a single integer, the hidden element.
Sample Input
3
1 2 1
0 -1 0
0 1 0
Sample Output
1
题意
给一个初始都为0的矩阵,进行若干次操作,每次操作给一行或者一列的每个数加一个正整数。操作结束后隐藏其中一个数,求出这个数。
思路
ai,j - ai,j+1 = ai+1,j - ai+1,j+1
签到题就是拼反应速度,我就没反应过来。。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int MAXN = 2e5 + 5;
int t, n, m, k, flag = 1;
int a[1005][1005];
int main(void) {
IO;
cin >> n;
int x, y;
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) cin >> a[i][j];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(a[i][j] == -1) {
x = i;y = j;
}
if(x == 1) {
if(y == 1) {
cout << a[1][2] + a[2][1] - a[2][2] << '\n';
}else {
cout << a[1][y - 1] + a[2][y] - a[2][y - 1] << '\n';
}
}else {
if(y == 1) {
cout << a[x][2] + a[x - 1][1] - a[x - 1][2] << '\n';
}else {
cout << a[x][y - 1] + a[x - 1][y] - a[x - 1][y - 1] << '\n';
}
}
}