CodeForces 463C - Gargari and Bishops

来源:互联网 发布:手机破解软件 编辑:程序博客网 时间:2024/06/05 05:44

Gargari is jealous that his friend Caisa won the game from the previousproblem. He wants to prove that he is a genius.

He has a n × n chessboard. Each cell of the chessboard has a number written on it.Gargari wants to place two bishops on the chessboard in such a way that thereis no cell that is attacked by both of them. Consider a cell with number x written on it, if this cell is attacked by oneof the bishops Gargari will get x dollars for it. Tell Gargari, how to place bishops on the chessboard toget maximum amount of money.

We assume a cell is attacked by a bishop, if the cell is located on thesame diagonal with the bishop (the cell, where the bishop is, also consideredattacked by it).

Input

The first line contains a single integer n (2 ≤ n ≤ 2000). Each of the next n lines contains n integers aij (0 ≤ aij ≤ 109) — description of thechessboard.

Output

On the first line print the maximal number of dollars Gargari will get.On the next line print four integers: x1, y1, x2, y2(1 ≤ x1, y1, x2, y2 ≤ n), where xi is the number of the row where the i-th bishop should be placed, yi is thenumber of the column where the i-th bishop should be placed. Consider rows are numbered from 1 to n from top to bottom, and columns are numberedfrom 1 to n from left to right.

If there are several optimal solutions, you can print any of them.

Sample test(s)

input

4
1 1 1 1
2 1 1 0
1 1 1 0
1 0 0 1

output

12
2 2 3 2

 

思路:

用f1,f2两个数组分别存储反对角线,正对角线上的数的和:

            f1[i + j] += a[i][j];

            f2[i - j + n] += a[i][j];

然后点(i,j)上两条对角线的和便为

            sum[i][j] = f1[i + j] + f2[i - j + n] - a[i][j];

 

比赛时候没看到Gargari wants to place two bishops on thechessboard in such a way that there is no cell that is attacked by both ofthem.即两只棋子对角线不会有交集-.-


代码:#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int N = 2005;__int64 sum[N][N];__int64 f1[N * 2], f2[N * 2];int a[N * 2][N * 2];int main(){int n;memset(f1, 0, sizeof(f1));memset(f2, 0, sizeof(f2));scanf("%d", &n);int i;for (i = 0; i<n; i++){int j;for (j = 0; j<n; j++){scanf("%d", &a[i][j]);f1[i + j] += a[i][j];f2[i - j + n] += a[i][j];}}for (i = 0; i<n; i++)for (int j = 0; j<n; j++)sum[i][j] = f1[i + j] + f2[i - j + n] - a[i][j];int x1, y1, x2, y2;__int64 max1 = 0, max2 = 0;x1 = x2 = y1 = 1;y2 = 2;for (i = 0; i<n; i++)for (int j = 0; j<n; j++){if ((i + j) & 1){if (max1<sum[i][j]){max1 = sum[i][j];x1 = i;y1 = j;}}else{if (max2<sum[i][j]){max2 = sum[i][j];x2 = i;y2 = j;}}}printf("%I64d\n", max1 + max2);printf("%d %d %d %d\n", x1 + 1, y1 + 1, x2 + 1, y2 + 1);return 0;}


0 0