Gargari is jealous that his friend Caisa won the game from the previous problem. 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 there is no cell that is attacked by both of them. Consider a cell with number x written on it, if this cell is attacked by one of the bishops Gargari will get x dollars for it. Tell Gargari, how to place bishops on the chessboard to get maximum amount of money.
We assume a cell is attacked by a bishop, if the cell is located on the same diagonal with the bishop (the cell, where the bishop is, also considered attacked by it).
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 the chessboard.
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 the number 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 numbered from 1 to n from left to right.
If there are several optimal solutions, you can print any of them.
4 1 1 1 1 2 1 1 0 1 1 1 0 1 0 0 1
12 2 2 3 2
题目大意:
给你一个N*N大小的格子,我们可以在上边放两个棋子,对应获得棋子所在两个对角线的价值,我们现在想要得到一个放置的方式,使得两个棋子没有共同攻击得到的位子存在
问怎样放置能够获得最大值。
思路:
1、思路很好建立,对应我们希望两个棋子没有重叠点,那么我们将图按照坐标(x+y)的奇偶性染成黑白两色。那么我们只要保证一个棋子放在黑色位子上, 另外一个棋子放在白色位子上即可,对应我们想要得到最大价值,那么我们只要每种情况的维护最大值即可。
2、那么我们求出每一种对角线的和,然后对应枚举点,维护最大值加和即可。
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
#define mid 3000
ll r[2005*4];
ll l[2005*4];
ll a[2005][2005];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%I64d",&a[i][j]);
l[i+j]+=a[i][j];
r[i-j+mid]+=a[i][j];
}
}
ll ans1=-1,ans2=-1;
int xx,yy,xxx,yyy;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
ll val=r[i-j+mid]+l[i+j]-a[i][j];
if((i+j)%2==0)
{
if(val>ans1)
{
ans1=val;
xx=i+1,yy=j+1;
}
}
if((i+j)%2==1)
{
if(val>ans2)
{
ans2=val;
xxx=i+1,yyy=j+1;
}
}
}
}
printf("%I64d\n%d %d %d %d\n",ans1+ans2,xx,yy,xxx,yyy);
}
}